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 01:57 AM · gameobjectposition

Get enemy position in script

I want to get the x,y,z position of a GameObject with the name Enemy in a script. The script is attched to a player object.

I get an error " The name `Enemy' does not exist in the current contextThe script is not attatched to the Enemy game object."

GameObject enemy = GameObject.Find ("Enemy");

//then i want to get x,y,z posotion of this Enemy

http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html

What am I doing wrong?

Comment
Add comment · Show 9
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 GC1983 · Jun 05, 2012 at 02:14 AM 1
Share

Help us out and post code snippets. Its hard to understand what exactly is going wrong when we have to wonder. That error pretty much means that the variable youre using isnt in the parameters of the function. Did you spell the Enemy name different in any way?

avatar image jagguy · Jun 05, 2012 at 02:18 AM 0
Share

There a cube I have name Enemy . This code below is attached to the player and it doesnt know what enemy is?

void Start () { GameObject enemy = GameObject.Find ("Enemy"); }

 // Update is called once per frame
 void Update () {
     
     enemyx=enemy.transform.position.x; //error here
 }
avatar image hijinxbassist · Jun 05, 2012 at 02:23 AM 0
Share

Finding GameObect....("Enemy"); will find one enemy named "Enemy" , so if the name of the object is not Enemy, the return will be null. Double check the actual name of the object jic.

avatar image jagguy · Jun 05, 2012 at 02:26 AM 0
Share

I have a Cube I called Enemy. The name Enemy appears in the hierarchy and in the inspector on top left.

avatar image GC1983 · Jun 05, 2012 at 02:27 AM 0
Share

are you sure its not 'enemy' ins$$anonymous$$d of 'Enemy'? that the error is looking at? You have the GameObject enemy locally declared in Start(). And then trying to execute it in a separate function; Update(). $$anonymous$$ake the declaration global and see what it does.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by jtbentley · Jun 05, 2012 at 02:22 AM

This should work, it's JS but minus the declarations it should work a treat.

 private var enemy : Transform;
 private var enemyX = 0.0; // Set this as a float
 
 function Start()
 {
  enemy = GameObject.Find("/Enemy").transform;
 }
 
 function Update()
 {
   if (enemy) enemyX = enemy.position.x;  // We check to make sure the object exists before we go pulling data from it
 }
Comment
Add comment · Show 1 · 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 jtbentley · Jun 05, 2012 at 02:28 AM 0
Share

You can also protect against the null on the Start() with try { enemy = GameObject.Find("/Enemy").transform; } catch (err) { print ("The object didn't exist"); }

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Compare Position of two gameobjects and if true, do nothing, help me please! 1 Answer

Different Position (Object position - Mouse Position) 1 Answer

Moving GameObject to various position ? 1 Answer

Initializing Prefab Position 2 Answers

Object's sprite and collider are not at same position as transform 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