- Home /
Question by
LifeSizeDeity · Oct 15, 2010 at 10:45 PM ·
objectaccessing
Having accessing problems
I made an objectA that has a script attached to it. I am accessing this objectA in a script that is located in another objectB. I am having trouble accessing the script of ObjectA from ObjectB. Could someone help, please.
Comment
Best Answer
Answer by skovacs1 · Oct 15, 2010 at 10:55 PM
You don't really describe enough to know what's wrong, so I'll just explain how to do what you describe in the general case and you can fix what you are doing wrong/differently.
scriptA.js (Attached to object A)
var foo : int = 42;
scriptB.js (Attached to object B)
function Start() {
var objectA : GameObject = GameObject.Find("Object A");
//It would be faster and more efficient if it were tagged
//and GameObject.FindWithTag("A"); were used,
//or you could just store it as a public variable.
var bar : scriptA = objectA.GetComponent(scriptA);
Debug.Log("The meaning of life, the universe and everything is "
+ bar.foo.ToString());
}
or in mono (C#)
scriptA.cs (Attached to object A)
public int foo = 42;
scriptB.cs (Attached to object B)
public void start Start() {
GameObject objectA = GameObject.Find("Object A");
//It would be faster and more efficient if it were tagged
//and GameObject.FindWithTag("A"); were used,
//or you could just store it as a public variable.
ScriptA bar = objectA.GetComponent<scriptA>();
Debug.Log("The meaning of life, the universe and everything is "
+ bar.foo.ToString());
}
Holy crap you rock, seriously all i did wrong was ScriptA bar = GetComponent(); ins$$anonymous$$d of ScriptA bar = objectA.GetComponent();
Thank you so much