x


Reference variable in other script

Hello,

I have an object that the player must hit, the object needs to be hit 3 times for something to happen, I want to be able to have several of these objects in my scene.

The problem I am having is referencing this hit value from another script.

The object I am hitting has a script to detect collisions, while another object has the scripts that are controlling its behaviour. I need to be able to get the hit counter from the collisions script.

more ▼

asked Aug 01 '10 at 11:18 AM

Troy gravatar image

Troy
32 8 8 9

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first
more ▼

answered Aug 01 '10 at 11:53 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

I receive a NullReferenceException when I try to run the game, I am using this code;

maledictorHealth = transform.Find("Maledictor").GetComponent(MaledictorHits).hits;

Aug 01 '10 at 12:58 PM Troy

Is the script on 'Maledictor' really called MaledictorHits?

Aug 01 '10 at 01:47 PM e.bonneville

Make sure an object with the named, and capitalized "Maledictor" actually exists when the line is run. If this is true, then try running:

if (!transform.Find("Maledictor")) { Debug.LogAlert("Maledictor was not found!"); }

If you get the warning "Maledictor was not found" in the log, then you may have mispelled Maledictor. If you don't get that error, try it again, but replace !transform.Find("Maledictor") with transform.Find("Maledictor").GetComponent(MaledictorHits). If you get a not found, then you may have misspelled the script name.

Aug 02 '10 at 03:41 AM Flynn

Finally, if you do not, I'm not sure what is going wrong.

Aug 02 '10 at 03:43 AM Flynn
(comments are locked)
10|3000 characters needed characters left

make that variable as public static. in another script acees though classname.varname

more ▼

answered Nov 23 '12 at 07:27 AM

ravindra123 gravatar image

ravindra123
1

(comments are locked)
10|3000 characters needed characters left

Ok, so I have tried to work with this script to get a variable out of another script...

Graphics is the object GUIButtons is the script test is the variable in the script I want to get the value of

function Update () { var test = transform.Find("Graphics").GetComponent(GUIButtons).test; //Then here I would like to print the value it finds for the variable "test" }

nothing I am doing is working. ANY help would be great!

more ▼

answered Nov 30 '11 at 11:53 PM

elrobbo6 gravatar image

elrobbo6
1 1 2 2

Post this as a new question - it's confusing to post a question as an answer to another question!

Nov 30 '11 at 11:57 PM aldonaletto
(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:

x3462
x2091
x263

asked: Aug 01 '10 at 11:18 AM

Seen: 3634 times

Last Updated: Nov 23 '12 at 07:27 AM