x


accessing gameObject script from an array

Sorry if this is too obvious, I'm still trying to learn how to scipt. Here's the problem I'm having. I have three boxes in the scene called box1, box2, box3. These boxes are in a gameobject array variable within a script that's attached to the camera.

Now each of these boxes have a script attached to them that switches a bool to off if I mouse over the box.

My question is, how do I access this script that's attached to these boxes through the script that's attached to the camera and within the array that these boxes are held at. I usually use a GameObject.Find to find info for one object, but never had to do it within an array. Any help would be great thanks!

more ▼

asked Jul 17 '11 at 05:48 AM

handy gravatar image

handy
1 1 1 2

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

3 answers: sort voted first

So long as you have a reference to the gameobject or transform (which it sounds like you have with your mouseover functionality), you can simply use GetComponent to get a reference to the component/script you want:

http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html

more ▼

answered Jul 17 '11 at 06:46 AM

testure gravatar image

testure
4.2k 20 25 48

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

In your camera script, probably have

var boxes : GameObject[]; // assign your boxes in inspector or code

... some function..

  var ms : MyScript = boxes[1].GetComponent("MyScript");
  ms.myBool = false; //or whatever

Note you can pre-fetch the components of those boxes in a Start script so you don't have to GetComponent each time.

more ▼

answered Jul 17 '11 at 07:22 AM

DaveA gravatar image

DaveA
26.5k 151 171 256

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

This script can called any tag"box" in gameObject script.

// Disable the spring on all HingeJoints 
// in this game object
var boxes : GameObject[];
var boxScript : boxScript;


function Start ()
{
    boxes = GameObject.FindGameObjectsWithTag("box");

    for (var i : int = 0; i < boxes.Length;i++)
    {
        boxScript[i] = boxObject[i].GetComponents("YourScript");
        boxScript[i].yourBoolean = false;`
    }
}
more ▼

answered Jul 17 '11 at 09:13 AM

YikYikHeiHei gravatar image

YikYikHeiHei
311 8 9 13

(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:

x1359
x355

asked: Jul 17 '11 at 05:48 AM

Seen: 1349 times

Last Updated: Jul 17 '11 at 09:13 AM