0
\$\begingroup\$

enter image description hereI want to access a Collider in c# script file with its tag name I Tried this

if (Collider2D == Physics2D.OverlapPoint(touchPos))
{
    Some Code to be Performed
}

I want to access this Collider2D with its Tag Name like this

if (GameObject.FindGameObjectWithTag("tag name") == Physics2D.OverlapPoint(touchPos))
{
    Some Code to be Performed
}

But Its not working Please Help...

public GameObject[] targets,targetss;

    void Start () {
        targets = GameObject.FindGameObjectsWithTag ("right screen");
        targetss = GameObject.FindGameObjectsWithTag ("left screen");
    }


    void Update () {
        if (Input.touches.Length <= 0) {

        } else {
            Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            Vector2 touchPos = new Vector2(wp.x, wp.y);

            foreach(GameObject target in targets)
            {
                if (target.collider2D == Physics2D.OverlapPoint(touchPos))
                {

                    GameObject.FindGameObjectWithTag("player2").transform.position = 
                    new Vector2(GameObject.FindGameObjectWithTag("player2").transform.position.x, wp.y);

                }
            }


            foreach(GameObject target in targetss)
            {
                if (target.collider2D == Physics2D.OverlapPoint(touchPos))
                {

                    GameObject.FindGameObjectWithTag("player1").transform.position = 
                    new Vector2(GameObject.FindGameObjectWithTag("player1").transform.position.x, wp.y);

                }
            }





        }
    }
\$\endgroup\$
1
\$\begingroup\$

Stated that your examples clearest statement is "Some Code to be Performed" ... You must do some things:

Not confuse classes with objects

Search all objects ( istance not class) like this

public GameObject[] targets;
targets = GameObject.FindGameObjectsWithTag("tagName");

foreach (GameObject target in target) {
// get collider of each object
       if (target.collider!= null){
           if (target.collider.OverlapPoint(touchPos)){
              //Some Code to be Performed
           }
       }
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ actually I have two colliders and have to perform tasks accordingly when touches performs on each collider. \$\endgroup\$ – Santosh Yadav Jul 24 '15 at 11:10
  • \$\begingroup\$ I suggest you editing your question integrating the additional information. That may help people to target your problem \$\endgroup\$ – dnk drone.vs.drones Jul 24 '15 at 11:21
  • \$\begingroup\$ please have a look now on updated question ! actually touch on collider is working but single touch at a time i want to move both sliders on single time that's why i have devided screen in two parts with 2 box collider but i am unable to move both colliders at a time...... please have a look on attached image to understand actual issue ... in image i want to move both the slider with both hands at a single time as this game is for android mobiles \$\endgroup\$ – Santosh Yadav Jul 24 '15 at 11:28
0
\$\begingroup\$

There is something to correct, in my humble opinion..

When you do

public GameObject[] targets,targetss;
    void Start () {
            targets = GameObject.FindGameObjectsWithTag ("right screen");
            targetss = GameObject.FindGameObjectsWithTag ("left screen");
        }

parhaps you should do instead

public GameObject targets,targetss;
void Start () {
        targets = GameObject.FindGameObjectWithTag ("right screen");
        targetss = GameObject.FindGameObjectWithTag ("left screen");
    }

just because I assume you want to have ONE collider per side.

Then would be interesting to understand what you want to achieve. Here I assume again you want the circle to detect collision with side bars.

If the circle is moved with physics you should add to the side bars an

OnCollisionEnter2D(Collider2D coll)

function to describe what is to be done when collisions occur.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.