- Home /
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.
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?
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.
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?
Could it be a problem with the cast-to-float? What type is go.position.XYZ?
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.
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);
}
}
What was the issue then? GameObject.Find returned wrong object, but LINQ query returns correct one???
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.
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.
Your answer
Follow this Question
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