Search Unity

Raycast from gameobject that does not have the script in

Discussion in 'Physics' started by Perillio, Jul 23, 2019.

  1. Perillio

    Perillio

    Joined:
    Feb 4, 2019
    Posts:
    18
    Dunno if this is the right Section and the right title but i will try the best to describe my Problem:

    i have a controller object containing an fbx mesh as children (so its holding another object which contains the meshes as children). I created a controllerscript, where all parts are set in (as public) to work with them in the script.
    Now i want to do a raycast from within the controllerscript, which starts from one of the meshes but without the need of a script in the mesh object.

    I tried:

    Code (CSharp):
    1.  
    2. public GameObject gun;
    3.  
    4. /* SOME MORE CODE HERE ... 800 lines that i didnt want to post in the forums, so i shortened it ;) */
    5.  
    6. RaycastHit hit;
    7.         if (Physics.Raycast(gun.transform.position, gun.transform.TransformDirection(Vector3.forward), out hit, 100f))
    8.         {
    9.            
    10.             Debug.Log(hit.transform.position);
    11.         }
    12.         else
    13.         {
    14.            
    15.             Debug.Log("NO TARGET");
    16.         }
    This code gives me just a nullreference on the line of the if statement.
    Any idea how to solve this problem or do i really need to have the raycastin script in that object ?

    greetings
    Stefan
     
  2. Ilumisoft

    Ilumisoft

    Joined:
    Feb 26, 2017
    Posts:
    46
    Seems like your gun GameObject is null. In that case you will get a NullReferenceException, because you are trying to access its transform component. Did you properly assign it via the inspector?
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Presumably this happens because you've not assigned the "gun" object. It should say in the actual error message in the console.
     
  4. Perillio

    Perillio

    Joined:
    Feb 4, 2019
    Posts:
    18
    The gun object is set in inspector, also its rotating with my cam so it cant be null.
    Code (CSharp):
    1. gun.localRotation = Quaternion.Lerp(gun.localRotation,y,turnSpeed * Time.deltaTime);
    This works fine :confused:
    So it must be something else.

    But your answers showed me that the main Question (Raycast from other object) is answered :D

    will try a debug.log if gameobject gun is null then, but i cant explain myself why another simple function (the rotation) works fine then in update void.


    UPDATE:
    Ok, raycast gets a null referece because of the gun. Funny thing is, when i do this in front of the Raycasts IF :
    Code (CSharp):
    1. Debug.DrawRay(gun.transform.position, gun.transform.TransformDirection(Vector3.forward) * 1000, Color.white);
    i can see the line from the gun to where it points to.
    I think the problem is, what is needed in an object to cast a ray at all as the gun is just the mesh. No collider, no rigidbody, no nothing (exept a material). Could it be, that it needs anything inside to get this working at all ?
     
    Last edited: Jul 23, 2019
  5. Ilumisoft

    Ilumisoft

    Joined:
    Feb 26, 2017
    Posts:
    46
    Could you post the actual error message?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    What is needed is what you're accessing i.e. a GameObject with a Transform (which is always there) so as was asked above, can you post the actually exception error. That should mention where the problem is.
     
  7. Perillio

    Perillio

    Joined:
    Feb 4, 2019
    Posts:
    18
    NullReferenceException: Object reference not set to an instance of an object tankController.Update () (at Assets/scripts/tankController.cs:946)

    Line 946 is: if (Physics.Raycast(gun.transform.position, gun.transform.TransformDirection(Vector3.forward), out hit, 100f))

    In the Moment of writing this, i think i found the error. The gun is there, thats right but there is nothing it can point on as the Tank is the only object in the scene. Now that i created a floor, it gives no error anymore. Strange thing is, when i point into the sky, there is also no error.
    I dont know what fixed it, but now it seems to work as the debug.log shows a position now.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Yeah, that makes no sense. If the "Tank" GameObject is the only thing in the scene then how can you have a "Gun" object? Is that a child object? Can you at least show the above script in the inspector? It should show a link to a real GameObject in the scene.

    Anyway, if you're happy with it working then that's cool too.
     
    Perillio likes this.
  9. Perillio

    Perillio

    Joined:
    Feb 4, 2019
    Posts:
    18
    The "Tank Object" is the main object. Maybe with this screenshots its more clear, my english is not the best.

    Now that the cube is there (as my floor) it works. Strange thing but now that it works im happy :)

    Bildschirmfoto von 2019-07-23 20-30-24.png
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    The NullReferenceException going away when you add a completely unrelated object is bizarre!

    Is the "T_Gun" the GameObject you show above as "gun"?

    But yeah, if you're happy with it now then happy to also leave it there. :)
     
  11. Perillio

    Perillio

    Joined:
    Feb 4, 2019
    Posts:
    18
    T_Gun is the public object yes. Its reffered as private object later on because i want to take the whole part into an own script (where it gets it from the main script but as private).

    Unity editor is often reacting weird since i use it on linux :D but mostly its strange errors at start which can be cleared out of the log and never appear again.

    Now i have just to find out, how to project a circle on hit.point which projects on the floor or is static in UI if there is nothing within 100 meters, but thats another problem which i start to work on another day :)