x


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

more ▼

asked Mar 14, 2012 at 12:22 PM

pr0te gravatar image

pr0te
1 6 4 4

Not great with C#, but try this instead:

public GameObject krudt;
GameObject theKrudt = GameObject.Find("Krudt");
krudtScript = theKrudt.GetComponent("Grid") as Grid;

You weren't referencing that the 'krudtScript' was a GetComponent from the other object. ;)

Mar 14, 2012 at 12:29 PM Kleptomaniac
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Mar 14, 2012 at 12:31 PM

Bunny83 gravatar image

Bunny83
74.5k 25 91 318

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, 2012 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:

public GameObject krudt;
GameObject theKrudt;

It seems krudt is a member variable of your script, but is it initialized anywhere?

theKrudt 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. Keep in mind 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. Maybe your object isn't named right? Maybe there are more than one objects with that name?

Mar 14, 2012 at 09:53 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4752
x1336
x1062
x21
x7

asked: Mar 14, 2012 at 12:22 PM

Seen: 978 times

Last Updated: Mar 14, 2012 at 09:53 PM