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 Stone-Legion · Sep 09, 2013 at 05:40 AM · gameobjecttransformgameobject.find

C# How to move GameObject calling it by GameObject.Find(name) from another GameObject?

I have the following code:

 GameObject gObj = GameObject.Find(go.name);
 gObj.transform.Translate(new Vector3((float)go.position.X, (float)go.position.Y, (float)go.position.Z));

Also tried and this doesn't seem to work either :(:

 GameObject gObj = GameObject.Find(go.name);
     gObj.transform.position = new Vector3((float)go.position.X, (float)go.position.Y, (float)go.position.Z);
 

Nonetheless it doesn't seem to move the game object. It finds it no problem, but it will not update the gameobject to the new vector coordinates.

Can someone please be so kind to tell me how to move a game object in this way, or similar, calling it by name? The script is being run from outside the game object it wants to move.

Thanks.

Comment
Add comment · Show 6
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 syclamoth · Sep 09, 2013 at 05:48 AM 0
Share

What exactly is 'go'? How and where does 'go.position' get set? Also, go.position does not seem to be a standard Vector3 either- what is the structure there?

avatar image Stone-Legion · Sep 09, 2013 at 06:13 AM 1
Share

I have a datastream co$$anonymous$$g in through TCPClient, want to update the gameobject position remotely. Because you can't use unityengine.dll outside, preventing me from using unity classes on my server, I had to write my own quick Vector class to serialize/deserialize. Long story short, even if the "go.position.X" was set to a hard coded number, it still doesn't work.

I've done a print(go.position.X+" "+go.position.Y+" "+go.position.Z) etc etc to see if the position is actually being passed, which it is, it's just that the gameobject doesn't actually go to the new position, it just stays at its current coordinates.

avatar image ArkaneX · Sep 09, 2013 at 07:13 AM 0
Share

I don't think this could be an issue, but... In code above you use uppercase X, Y, Z, but in print command from comment you use lowercase letters. Is it by mistake, or maybe you have separate properties/fields and they return different values?

avatar image syclamoth · Sep 09, 2013 at 07:35 AM 0
Share

Could it be a problem with the cast-to-float? What type is go.position.XYZ?

avatar image Stone-Legion · Sep 09, 2013 at 07:49 AM 0
Share

Just to be sure, I've changed it to float so it is all now strictly float, previously it was a decimal. Still no go.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Stone-Legion · Sep 09, 2013 at 08:58 AM

Got it, instead of using GameObject.Find() I just created a LINQ GameObject list and search through it with LINQ commands instead. Works fine so far, just a quick test really, but glad to see I can use LINQ with this. :)

 using System.Linq;
 
 IList<GameObject> gameObjectList = new List<GameObject>();
 public GameObject character;
 //....
 void UpdateOrAddGameObjectPosition(string name, Vector3 vector)
 {
   var record = (from gol in gameObjectList where gol.name == name select gol).FirstOrDefault();
   if(record != null)
   {
        record.transform.position = vector;
   }
   else
   {
      GameObject gObj = (GameObject)Instantiate(character,vector,Quaternion.identity);
      gObj.name = name;
      gameObjectList.Add(gObj);
   }
 }
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 ArkaneX · Sep 09, 2013 at 09:17 AM 0
Share

What was the issue then? GameObject.Find returned wrong object, but LINQ query returns correct one???

avatar image syclamoth · Sep 09, 2013 at 09:21 AM 1
Share

This is probably faster, and safer than the GameObject.Find method anyway.

In general, avoid using any of the 'string-literal' based object searching systems provided with Unity- there are probably 3 or 4 questions asked on this site every day involving problems caused by those functions, and in almost all of them the problem can be solved by indexing the objects in another way.

avatar image Stone-Legion · Sep 09, 2013 at 09:39 AM 0
Share

Thanks syclamoth.

The issue seemed to be that Unity didn't like instantiating a game object and then using GameObject.Find() to find the object and update the position of it. The other way I was doing it was pretty slow and not a good way to do it, then again, I was just messing around with it as I learn more. Before I just had a loop creating game objects then trying to find them later with GameObject.Find() to update the position rather than saving the GameObjects to a list. Ins$$anonymous$$d, now I'll just save them to memory with an IList and access by LINQ.

Previously, GameObject.Find was returning the right object, but the position wasn't updating, strangely.

Thanks to everyone that took the time to review this question, it's great the community has some awesome ppl here and I'm sure I'll be asking many more questions in the future - and hopefully answering some other ppls q's along the way.

avatar image ArkaneX · Sep 09, 2013 at 09:55 AM 0
Share

Thanks for explanation why it didn't work :)

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

17 People are following this question.

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

Related Questions

Finding object with transform 1 Answer

Instantiate rigidbody, transform or gameobject? 1 Answer

I want to move a cube with rotation but I find this problem 1 Answer

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers

Is there any way to convert Collision to a Gameobject or 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