Search Unity

Help with collision dections and names

Discussion in 'Scripting' started by Arqueiro, Jun 30, 2015.

  1. Arqueiro

    Arqueiro

    Joined:
    Jun 26, 2015
    Posts:
    9
    Hello every one,

    Im trying to make a script to change a ui text and a material when I have my crosshair over some specific object here is what I have:


    void Update ()

    {
    Vector3 frente = transform.TransformDirection(Vector3.forward) * distanceToSee;
    Debug.DrawRay(this.transform.position, frente, Color.black);

    if(Physics.Raycast(this.transform.position, frente, out justHitsmt, distanceToSee))
    {

    if (justHitsmt.collider.gameObject.name == "LUZ_LUMINARIA");
    {

    Debug.Log(justHitsmt.collider.gameObject.tag);
    Texto.text = " oiiiii";

    if(Input.GetButtonDown("Fire1")) //AO PRECIONAR O BOTAO DO JOYSTICK OU MOUSE 1
    {


    justHitsmt.collider.gameObject.GetComponent<Renderer>().material = mat[index];
    index = (index +1) % mat.Length;


    }



    }

    }

    }
    }

    the change material part is ok, but the text is changing even when I point to others colliders...


    any help ?

    thanks in advance!
     
  2. Arqueiro

    Arqueiro

    Joined:
    Jun 26, 2015
    Posts:
    9
    One more thing I forgot to mention, I used debug.log to confirm, and really is changing when it collides with other objects.
     
  3. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518

    Wow this is the second time I have fixed this kind of problem in a week.

    LOOK AT THE END OF THE IF - YOU HAVE AN EXTRA SEMICOLON ;


    This makes the if statement finish on that line. That means your text setting is being set anytime your raycast is true (it is ignoring the name check)

    Also in future, please use code tags for code posts.
     
    Arqueiro likes this.
  4. Arqueiro

    Arqueiro

    Joined:
    Jun 26, 2015
    Posts:
    9
    lol

    Oh my good, I cant belive it, thanks im soo noob....
     
    Korno likes this.
  5. Arqueiro

    Arqueiro

    Joined:
    Jun 26, 2015
    Posts:
    9
    now its working but I have another problem, I used a else statement to change the text to " " when I move away from the collider, but this cause the text to be always empty.

    I used,

    else
    {
    Texto.text = " ";
    }
     
  6. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
  7. Arqueiro

    Arqueiro

    Joined:
    Jun 26, 2015
    Posts:
    9
    jut figure out, I was putting the else in the wrong place, thanks again, you really help me.
     
    Korno likes this.