Search Unity

Raycast In C# Doing Nothing

Discussion in 'Scripting' started by itscloudy614, Aug 4, 2010.

  1. itscloudy614

    itscloudy614

    Joined:
    Jul 15, 2010
    Posts:
    4
    Code (csharp):
    1. void BuildingCallout() {
    2.        
    3.         RaycastHit hit = new RaycastHit();
    4.         Ray ray = new Ray();       
    5.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.        
    7.         if (Input.GetMouseButtonDown (0))  //Returns true during the frame the user touches the object
    8.         {
    9.             Debug.Log("mouse down");
    10.             if (Physics.Raycast (ray, out hit))
    11.             {
    12.                 Debug.Log("pew pew");
    13.                 if (hit.collider.gameObject.name.Equals("BrooksCallOut"))  
    14.                 {
    15.                     Debug.Log("go brooks");
    16.                     BrooksCallOut.gameObject.SendMessage("gotoBrooks");
    17.                 }
    18.             }
    19.         }
    20.         else {
    21.         }
    22.  
    23.     }
    So here's my code. Its pretty strait foward, a raycast looks to the collider and if it hits a specific gameObject, it executes the function "gotoBrooks". I don't get any errors when I run the script but in the player, not a single one of the debug logs print. Does anyone have any idea what could be going on?
     
  2. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    Where do you call BuildingCallout from? Update would seem to be the obvious place, but you don't specify.

    Additionally, some tips:
    Code (csharp):
    1. Ray ray = new Ray();
    2. ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    can be collapsed into
    Code (csharp):
    1. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    and if you move
    Code (csharp):
    1. RaycastHit hit = new RaycastHit();
    2. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    inside "if (Input.GetMouseButtonDown(0))" you won't have to create them every frame unless the user clicks the mouse.
     
  3. itscloudy614

    itscloudy614

    Joined:
    Jul 15, 2010
    Posts:
    4
    So looking a bit further into it, it seems that even though I am defining the gameObject,
    Code (csharp):
    1. public GameObject BrooksCallOut;
    and the object is connected in the inspector, its not being recognized.
     
  4. burnumd

    burnumd

    Joined:
    May 27, 2008
    Posts:
    367
    I'm not sure I entirely understand what you mean or how you have things set up. Is "BrooksCallOut" an instance variable? If so, consider changing it to "brooksCallOut" (by convention, variables start with lowercase and classes or functions start with uppercase). It also appears you're never assigning it anywhere when you get your hit. Try
    Code (csharp):
    1. if (hit.collider.gameObject.name.Equals("BrooksCallOut"))  
    2. {
    3.     Debug.Log("go brooks");
    4.     BrooksCallOut = hit.collider.gameObject;
    5.     BrooksCallOut.SendMessage("gotoBrooks");
    6. }
     
  5. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    When using "out" methods, you shouldn't initialize the variable you're assigning to. That is:
    Code (csharp):
    1. RaycastHit hit = new RaycastHit();
    2. change to:
    3. RaycastHit hit;
    It may not be the culprit, but this is the proper procedure.
     
  6. itscloudy614

    itscloudy614

    Joined:
    Jul 15, 2010
    Posts:
    4
    Well the problem seems to have been some mystical gnome trickery. I deleted the gameObjects, reimported them, and reassigned all the script/inspector links and now it works.

    Thank you all for your help.