x


Changing gameobject assigned to a variable on input.getkey.

Hi!

Right, I'm using the flocking script, which has a variable called chasee. The value of this variable is assigned to a GameObject, which is selected in the inspector.

What I want to do is, using an If statement, change the gameobject that is associated with this variable on a keypress

I'm fine with structuring everything but how I would phrase changing the chasee variable.

Its in javascript btw.

more ▼

asked Nov 13 '09 at 12:54 PM

therussmorris gravatar image

therussmorris
51 8 9 16

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

2 answers: sort newest

You'll have to get a reference to the game object you want to change it to. There are many different ways to do this. You could either have two variables you set in the inspector or use any of the different Find() methods.

Using two inspector variables could look like this:

var chasee :GameObject;
var otherTarget :GameObject;

function Update() {
    // Cycle between targets
    if (Input.GetKey(KeyCode.Return)) {
        var oldTarget = chasee;
        chasee = otherTarget;
        otherTarget = oldTarget;
    }
}
more ▼

answered Nov 13 '09 at 02:34 PM

Adrian gravatar image

Adrian
136 1 1 4

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

I did a quick forum search for the flocking script and came up with this link...

http://forum.unity3d.com/viewtopic.php?p=156950&sid=c39b8da01f2f8b5be69af09409e483fa (3rd post from top)

Chasee is a private variable in the script. So I am guessing you've modified it to public in order to access it via the inspector.

Anyways assuming the flocking script is a child of some gameobject, here's how i would go about finding the gameobject, and setting the chasee parameter on its flocking script child object...

    GameObject myGameObject;

    GameObject objectWithFlockingScript = GameObject.FindGameObjectWithTag("someTag");

    var other : FlockingScript = gameObject.GetComponent(FlockingScript);        

    if (Input.GetKey("w"))
    {
        scriptToAccess.casee = myGameObject;
    }
more ▼

answered Nov 13 '09 at 02:45 PM

abdullah.ahmed gravatar image

abdullah.ahmed
252 5 7 16

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

x5081
x2087

asked: Nov 13 '09 at 12:54 PM

Seen: 2455 times

Last Updated: Nov 20 '09 at 01:03 PM