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 Karsnen_2 · Dec 09, 2011 at 07:00 AM · javascriptgameobjecttransformposition

Position of a GameObject

Hello,

I was wondering to find the position of a gameobject with a certain tag on the 'x' co-ordinate. I wrote a code and found that it did not give the answer I was expecting.

 var player = GameObject.FindGameObjectsWithTag ("Player");
 var dis : float;
 function Update () 
 {
 dis = player.transform.position.x;
 print(dis);
 }

Request:

Is my code right, if not how could I find a position of a gameobject which is not attached onto a script.

Thank you very much.

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 dannyskim · Dec 09, 2011 at 07:10 AM 0
Share

is the GameObject.FindGameObjectsWithTag line of code in your variable declarations or is it inside of a method?

avatar image Karsnen_2 · Dec 09, 2011 at 07:33 AM 0
Share

WEll the code I posted above is the entire script. I was testing with it. $$anonymous$$y sole aim is to find the position of a gameobject through script attached to another gameobject. Say I have enemy prefab I wish to find it's position.

COUld you please help me on this.

avatar image hatzalex · Dec 09, 2011 at 09:02 AM 1
Share

Well if you are instantiating a prefab you can always just ask the position of your clone. Otherwise try GameObject.FindWithTag("Player"), since you are looking for just one player (i assume).

avatar image Karsnen_2 · Dec 09, 2011 at 06:26 PM 0
Share

How do I ask the position of my clone? That is where I think I am lacking.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ks13 · Dec 09, 2011 at 09:18 AM

Actualy, what you're caling is GameObject.FindGameObject*s*WithTag ("Player"). With an "s" at the end of Objects. That means your player is an array even if there is ony one object. So you should do dis = player[0].transform.position.x;

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 Karsnen_2 · Dec 09, 2011 at 06:32 PM 0
Share

How do I ask the position of my clone? That is where I think I am lacking.

I changed and it works but I want to call the position of it's clone? That is where I am not sure.

avatar image
0

Answer by hatzalex · Dec 09, 2011 at 06:51 PM

Asking the position of a instatiated clone is pretty simple.

var clone : GameObject; var player: GameObject;

clone = Instantiate(player,transform.position,transform.rotation);

print (clone.transform.position);

The last line gets the current position of the clone.

Also take a look at instantiate for more info:

Instantiate

Edit: Btw, you said it does not give the answer you are expecting, but what answer are you expecting?

Comment
Add comment · Show 3 · 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 Karsnen_2 · Dec 09, 2011 at 07:40 PM 0
Share

var clone : GameObject; var player : GameObject;

Are both of these the same Prefab?

YOu will get the print correctly but I think as you refer the prefab on the project folder it gets its position and not the it's virtual clone.

avatar image hatzalex · Dec 09, 2011 at 07:45 PM 0
Share

You store the Intatiated player into the clone variable. That means that "clone" is empty until you store the Instantiated player variable in it. "Clone"is literally a clone of the player gameobject.

In the inspector you assign the player gameobject to the player var, but you leave the clone variable empty, since you must store the Instantiated player in it.

When you ask the position of the clone, you actually ask the position of the player you just Instantiated.

avatar image ks13 · Dec 10, 2011 at 03:51 PM 0
Share

Not sure he understood that...
Let's explain it this way :
"player" is the model of what you want to copy
"clone" is the adress of the copied object
the same way you put an int into a variable, "clone" contains the newly created object object, without it you don't know where the information fo the clone is stored and can't use it.
Not much better of an explanation but hope you understood how it works now.

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

7 People are following this question.

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

Related Questions

Instantiate as a child at position 2 Answers

Getting an Objects position once 2 Answers

How To Move GameObjects In Fixed Path? 1 Answer

How to tell if two blocks are right next to each other?(2D) 1 Answer

Transform Checking on all Array Objects (JS) 3 Answers


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