hello,
i am writing a script with an IEnumerator, the first script creates a line render through a raycast… in which if the target ball is hit by a that raycast it should produce another lineRender from the object itself…
my code is producing the error:
NullReferenceException: Object reference not set to an instance of an object
BallRayKaster.Update () (at Assets/Scripts/Raycasting Scripts/BallRayKaster.cs:26)
why would this happen if the code seems allright to me…
here is a snipet of the code:
any help would be appreciated…
also if one may explain what null-reference actually means practically…
Please use code tags and then copy/paste the text - Images don’t work well here.
From the code I see, you did not initialize the private variable ‘line’ in start, so accessing ‘line.enabled’ will produce the null reference.
Also, note that stringing lots of references together like
cueBallRayCaster.getHitInfo().transform.gameObject
has three possibilities of failing with null: cueBallRayCaster, getHitInfo, transform. The first Thing to do when faced with such an error is to take apart compund statements and log the individual parts with Debug.Log() to make sure they are all initialized.
1 Like
Try using Visual Studio or any other IDE that is able to hook itself onto Unity for debugging. There, you can set breakpoints and check line for line what object is null and what exactly throws the exception.
Since the exception is thrown in line 26, either line is not set to reference a LineRenderer (are you missing a [SerializeField] for the member line in line 12, perhaps?) or getHitInfo() didn’t actually hit something, which means that collider is null, but you try to access its member gameObject.
In C#, you get access to objects either by reference or by value, see Passing Parameters (C# Programming Guide), Reference types (C# Reference) and Value types (C# Reference) for instance. Without going too much into technical detail what it means, imagine it like this:
You know of a house (object) and you know where it is located, adress, street, town and so on (reference). If you walk there, to that adress, then you expect to find the house. Now you stand right in front of it, you can touch it, alter it, whatever you want to do with it (value).
Now think of a friend telling you of a house (object) and its adress (reference). You walk there, but you don’t see any house at the adress that your friend gave you. You encounter a NullReferenceException, as you expected a house (object) there, but there is none. In your code, if you want to open the door of the house (calling an object method of the object house), but there is no actual house (object), your code doesn’t know what to do and throws a NullReferenceException. After all, the code expected a house (object) to be there because you told it there would be one by calling the method to open the door of the house.
1 Like
thank you for your explanations :), i was able to solve the solution… i simply did not reference the line renderer for line