I have now read all of the threads on how to use GetComponent for Unity iPhone. I must be either tired or thick: I’m not grokking it.
Imagine I have a game object called “GlobalVars”. I want to change the value of var “pickups” inside a script attached to GlobalVars from a script (call it “foo.js”) on a different game object. So I have:
script foo.js:
var globNode : GameObject;
function Start (){
globNode = GameObject.Find("GlobalVars");
globNode.GetComponent(global).pickups += 1;
}
This gives me the error:
Assets/_Scripts/GameScripts/StarPickup.js(24,35): BCE0019: 'pickups' is not a member of 'UnityEngine.Component'.
Can someone please give me an example of how this should be (re)written? I am stuck and would sincerely appreciate someone’s help.
You cant reference the component directly with Strong Typing as you are “talking” to a component of one type (global), while the base of the call is a component of another type (GameObject), so you need an extra step in there.
You need to create a var specifically of type global to reference the item inside it.
var globNode : GameObject;
function Start (){
globNode = GameObject.Find("GlobalVars");
var globals : global = globNode.GetComponent(global);
globals.pickups += 1;
}
var playerNode : GameObject;
function Start() {
playerNode =GameObject.Find("Player");
}
function Update () {
if (playerNode.active) Object.SetActiveRecursively (true);
}
Object isnt defined as anything. If it is the playerNode you are trying to turn on (with all of its children) then just use this.
var playerNode : GameObject;
function Start() {
playerNode =GameObject.Find("Player");
}
function Update () {
if (playerNode.active) playerNode.SetActiveRecursively(true);
}
I’m really not quite sure what you are trying to achieve with the script above.