- Home /
Get vector3 from another script
Hey guys.
I'm having some problems with getting a vector3 variable from another script.
public Vector3 krudtPosition;
In Update(), I'm running this next script through once, using a bool checking if it has just been done.
krudtPosition = new Vector3(30.50F,krudt.transform.position.y,krudt.transform.position.z);
This works fine when I debug it. The problem comes when I want to get it in another script.
First I define this.
public Grid krudtScript;
Grid is the name of the script where I want to get the vector3 from.
Then, in the Start() i do the following:
public GameObject krudt;
GameObject theKrudt = GameObject.Find("Krudt");
krudtScript = GetComponent("Grid") as Grid;
And lastly, I'm using it like this:
krudt.transform.position = krudtScript.krudtPosition;
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
Not great with C#, but try this ins$$anonymous$$d:
public GameObject krudt;
GameObject the$$anonymous$$rudt = GameObject.Find("$$anonymous$$rudt");
krudtScript = the$$anonymous$$rudt.GetComponent("Grid") as Grid;
You weren't referencing that the 'krudtScript' was a GetComponent from the other object. ;)
Answer by Bunny83 · Mar 14, 2012 at 12:31 PM
That's simply because you implicitly use:
this.GetComponent()
instead of
theKrudt.GetComponent()
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;
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:
public GameObject krudt;
GameObject the$$anonymous$$rudt;
It seems krudt
is a member variable of your script, but is it initialized anywhere?
the$$anonymous$$rudt
looks like a local variable inside your function.
If the Grid
script is attached to the object you want to move, why don't you assign the position inside that script?
A NullReferenceException happens when you try to use / access a reference variable, but it's not initialized. $$anonymous$$eep in $$anonymous$$d all those functions like GameObject.Find() or GetComponent() can return null
if the object doesn't exist.
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. $$anonymous$$aybe your object isn't named right? $$anonymous$$aybe there are more than one objects with that name?
Your answer
Follow this Question
Related Questions
Distance Variable Won't Change. What's Wrong With My Script? 1 Answer
Can't access variable from another script. 1 Answer
Speed updated from another script, but doesn't persist 1 Answer
addition of vector3 1 Answer
Remaping values? 1 Answer