x


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.

more ▼

asked Oct 15 '10 at 10:45 PM

LifeSizeDeity gravatar image

LifeSizeDeity
127 15 17 26

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

1 answer: sort voted first

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());
}
more ▼

answered Oct 15 '10 at 10:55 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

Holy crap you rock, seriously all i did wrong was ScriptA bar = GetComponent(); instead of ScriptA bar = objectA.GetComponent();

Thank you so much

Oct 15 '10 at 11:37 PM LifeSizeDeity
(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:

x3318
x1089
x46

asked: Oct 15 '10 at 10:45 PM

Seen: 433 times

Last Updated: Oct 15 '10 at 10:45 PM