Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jagguy · Jun 05, 2012 at 02:58 AM · positionenemy

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); 
     }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jtbentley · Jun 05, 2012 at 03:10 AM 0
Share

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;

avatar image jagguy · Jun 05, 2012 at 03:12 AM 0
Share

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

avatar image Berenger · Jun 05, 2012 at 03:15 AM 0
Share

GameObject.FindGameObjectsWithTag

avatar image jtbentley · Jun 05, 2012 at 03:16 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jtbentley · Jun 05, 2012 at 03:20 AM

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Berenger · Jun 05, 2012 at 03:23 AM 0
Share

$$anonymous$$eep in $$anonymous$$d 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.

avatar image jagguy · Jun 05, 2012 at 04:15 AM 0
Share

I should be using find by tag ins$$anonymous$$d as find is the slowest find function? How do I use code ins$$anonymous$$d of using the find function?

avatar image Berenger · Jun 05, 2012 at 04:24 AM 0
Share

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.

avatar image jagguy · Jun 05, 2012 at 04:32 AM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemy Moves Jitters when Supposed to Be Still 0 Answers

Predict enemy position in X seconds? 1 Answer

Photon Syncing Enemies with Other Clients? 1 Answer

Return Enemy To Start Position 1 Answer

How to Return Enemy to startPosition 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges