|
Hey guys. I'm having some problems with getting a vector3 variable from another script. In Update(), I'm running this next script through once, using a bool checking if it has just been done. This works fine when I debug it. The problem comes when I want to get it in another script. First I define this. Grid is the name of the script where I want to get the vector3 from. Then, in the Start() i do the following: And lastly, I'm using it like this: I've also tried this, but to no luck: krudt.transform.position = new Vector3(krudtScript.krudtPosition); I'm getting this error: NullReferenceException: Object reference not set to an instance of an object
(comments are locked)
|
|
That's simply because you implicitly use:
instead of
So you search for this script on the gameobject this script is attached to and not on the other where it actually is. Even with that done, I'm still getting the same error: NullReferenceException: Object reference not set to an instance of an object at this line: krudt.transform.position = krudtScript.krudtPosition;
Mar 14 '12 at 08:44 PM
pr0te
Ok you posted quite unrelated and very confusing code snippets. You should make yourself clear about your setup, on which object is which script and then how to access those objects. You have two variables: It seems
If the A NullReferenceException happens when you try to use / access a reference variable, but it's not initialized. Keep in mind all those functions like GameObject.Find() or GetComponent() can return You have to debug your scripts yourself. Use some Debug.Log() and print the value of the variables to know which one is null. When you figured this out you can search for the reason. Maybe your object isn't named right? Maybe there are more than one objects with that name?
Mar 14 '12 at 09:53 PM
Bunny83
(comments are locked)
|

Not great with C#, but try this instead:
You weren't referencing that the 'krudtScript' was a GetComponent from the other object. ;)