Search Unity

problem with hiding/showing objects?

Discussion in '2D' started by jihen007, Feb 22, 2017.

  1. jihen007

    jihen007

    Joined:
    Feb 22, 2017
    Posts:
    8
    I have read many Unity posts and seem to have the issue still after trying all the suggested solutions.

    Before I explain my prob, I will give a overview of what I am trying to do, in case there is some other solution that makes more sense rather than what I am trying to do.
    I have something like this

    and i've already secceeded to hide lines so now when i display i have this:


    and I want to show a line when the mouse is clicked .()

    I tried to add a box collider for each hidden line then use Raycast as a "google advice" . so ,
    this is my code but it dosent work with me ! i'm using debug.log to know where it bugs it just shows :
    Mouse is down !
    Origin: (-1.1, 1.2, -9.7), Dir: (0.0, 0.0, 1.0)
    UnityEngine.Debug:Log(Object)
    Code (CSharp):
    1. if (Input.GetMouseButtonDown (0)) {
    2.             Debug.Log("Mouse is down!");
    3.             RaycastHit hit = new RaycastHit ();
    4.             Ray ray = Camera.main.ScreenPointToRay (mouseposition);
    5.             Debug.Log(ray);
    6.             if (Physics.Raycast (ray,out hit))
    7.             {
    8.                 Debug.Log ("It's working!");
    9.                 Debug.Log("Hit " + hit.transform.gameObject.name);
    10.             }
    11.         }
    12.     }
     

    Attached Files:

    • Capture.PNG
      Capture.PNG
      File size:
      141.5 KB
      Views:
      946
    • 1.PNG
      1.PNG
      File size:
      55.2 KB
      Views:
      934
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    How are you hiding the lines? If you're setting the gameobjects inactive their colliders will no longer work.
     
  3. jihen007

    jihen007

    Joined:
    Feb 22, 2017
    Posts:
    8
    i made a list having the lines i want to hide then i coded this
    Code (CSharp):
    1. for (int i = 0; i < 16; i++) {
    2.             lines [i].SetActive (false);
    3.         }
    even i tried to change it 'true' to test but it dosen't detect the object.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Oh, of course. You should be using Physics2D.Raycast to raycast 2D colliders. You'll have to change your logic a little bit, as it doesn't return a boolean, it returns a RaycastHit2D. So you should then check if the raycastHit.collider is null.