|
i have to scripts and the first one neads to get a variable value from the second however although the value i want is public it dosent get it it comes up null if i print it and i dont have a clue why
second script
any help is aprichiated
(comments are locked)
|
|
It's the good old casting problem. GetComponent returns a reference of type Component. You either have to cast it explicit or implicit into the right type.
Now edit I actually don't understand why you use an array of Transform when you just want to access your score script. That way it's faster and much more reliable. It's the straight forward way: ime afraid this dident work
May 09 '11 at 07:01 PM
joseph b
Are you sure that all of your objects have the score script attached? If you try to get the a component that isn't there you will of course get a null reference.
May 09 '11 at 08:05 PM
Bunny83
I have added another solution. I guess your problem is that the GameObject that have this script attached is also returned by GetComponentsInChildren. If you run the game in the editor you should be able see all objects that have been returned in your inspector (since your open array variable is public). if you click on one element it gets highlighted in the hierarchy. I'm sure there is one object that doesn't have the score script attached. I'm pretty sure it's the one with the other script.
May 09 '11 at 08:45 PM
Bunny83
thanks that worked and one more question can you get the transform of the scripts game object ??
May 10 '11 at 12:52 PM
joseph b
Sure, every component have all the default shorthand properties like
May 10 '11 at 01:32 PM
Bunny83
(comments are locked)
|
