x


change properties of gameObject

Hi,

Given I can find a single enemy.

q1)will I run into trouble by changing this objects properties here in a script (not attached to it ) after I have accessed the object ? I could now change the position, how it moves, destroy it , disable one of its components etc. Am I getting a copy of this object or a pointer to the object would be how programmer of c++ would ask this question.

q2) to access a list of objects is there another function to use? I might have 10 enemy objects.

private GameObject myenemy;

public class SuperClass : MonoBehaviour // Don't forget to declare a class in C#
{
    void Start (){   
         myenemy = GameObject.Find ("Enemy");
    }   

    // Update is called once per frame
    void Update () {
        myenemy.transform.position = new Vector3(0, 0, 0); 
    }
}
more ▼

asked Jun 05 '12 at 02:58 AM

jagguy gravatar image

jagguy
30 6 26 36

myenemy = GameObject.Find("Enemy") would make myenemy a pointer, it is not a duplicate.

You can put a component (script) onto the object and cache the pointer to that too, so then you could do something similiar to..

enemy = GameObject.Find("Enemy").GetComponent(EnemyBehavior) as EnemyBehavior;

And then address it like..

enemy.health = blah;

Jun 05 '12 at 03:10 AM jtbentley

ok thanks for that. is there a way to get a list of objects like every enemy object?

Jun 05 '12 at 03:12 AM jagguy

I personally prefer to instantiate everything in code, so I always explicitly control what's in the scene.. Searching for objects in the hierarchy is a fairly sloppy practice - but it's fine for prototyping.

Jun 05 '12 at 03:16 AM jtbentley
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Short answer, that makes the equivalent of a pointer.

To disable components, you can utilise something akin to...

myEnemy = GameObject.Find("Enemy").GetComponent(BehaviorScript);
myEnemy.enabled = false;

This page lists the basics of adding components etc... But this page (Unity Manual) goes into much more detail about components/linking etc.

more ▼

answered Jun 05 '12 at 03:20 AM

jtbentley gravatar image

jtbentley
579 3 4 16

Keep in mind that Find( name ) is the slowest find function. By type or tag is much better. Even then, most of the time they can be avoided by a good design.

Jun 05 '12 at 03:23 AM Berenger

I should be using find by tag instead as find is the slowest find function? How do I use code instead of using the find function?

Jun 05 '12 at 04:15 AM jagguy

Can declare the var as public to initialize it in the editor, or use a singleton. There probably is other ways, but it's late.

Jun 05 '12 at 04:24 AM Berenger

You mean declare the Enemy object as public somewhere somehow? I dont know where you do this?

Why is the find function an issue with performance anyway? If I declare the variable to find something in start () then in update this should be an issue?

Jun 05 '12 at 04:32 AM jagguy
(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:

x915
x671

asked: Jun 05 '12 at 02:58 AM

Seen: 487 times

Last Updated: Jun 05 '12 at 04:32 AM