|
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.
(comments are locked)
|
|
Short answer, that makes the equivalent of a pointer. To disable components, you can utilise something akin to... This page lists the basics of adding components etc... But this page (Unity Manual) goes into much more detail about components/linking etc. 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)
|

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;
ok thanks for that. is there a way to get a list of objects like every enemy object?
GameObject.FindGameObjectsWithTag
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.