- Home /
How to move one object's position to another in unity? (c#)
How do you move one object's position to another in unity? I need to make my player object move to the spawn point when he dies. Here is the code I am using:
this.transform.position = spawnpointsscript.gameObject.transform.position;
I am having an error with this because I am putting this on a spawn point prefab, because I have multiple levels and want to be able to easily make copies of it. The error says:
An object reference is required to access non-static member `UnityEngine.Component.gameObject'
I am guessing this is because I put it on the prefab instead of the spawn point itself. Is there a way to do this without having to put the script on every individual object, or is that the only way?
If you have a single spawnpoint in the scene, what you could do is add a tag for your spawnpoint like SpawnPoint and find that object in your script like this:
GameObject spawnpoint = GameObject.FindGameObjectsWithTag("SpawnPoint");
and then use
this.transform.position = spawnpoint.transform.position;
this script needs to be on your player.
Thank you for the answer it seems like it would work, but 2 error messages come up :
You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. If you are using C# don't use this function in the constructor or field initializers, Ins$$anonymous$$d move initialization to the Awake or Start function.
Cannot implicitly convert type UnityEngine.GameObject[]' to
UnityEngine.GameObject'
GameObject spawnpoint = GameObject.FindGameObjectsWithTag("Spawnpoint");
Should be
GameObject spawnpoint = GameObject.FindGameObjectWithTag("Spawnpoint");
Answer by YoungDeveloper · Nov 22, 2013 at 08:10 AM
This have been asked hundred times.. Check out related questions i already gave answers to:
http://answers.unity3d.com/questions/534933/random-spawn-random-prefab.html
http://answers.unity3d.com/questions/553385/enemy-spawning-system-in-c.html
I tried both of the links but the question they're asking is how to spawn (as in create) something in at the spawn point. I just want to make the player jump to the position of the spawn point. (Actually 5 above it but I know how to easily do that). The second one almost worked, but I cannot destroy and create the player (That would create problems) and recreate it using instantiate, I just want to move it. I had the game set up where it jumped to the position (0,5,0) but that means I would have to have the entire level around the (0,0,0). I just wanted to swap out (0,5,0) to the position of the spawn point (where ever I have it) + 5 in y, without messing up any of the scripts. Thank you for answering, though.
Edit: Oh, sorry. I didn't see someone answered it as a comment in my question.
Edit 2: Actually I thought about it and found the best way to do this would actually be to restart the level, and now it works fine. I'll mark the question as answered even though I didn't find the answer I was looking for.
Answer by the1337est · Aug 13, 2015 at 07:58 AM
Create an empty GameObject named SpawnPoint and place it where you want your spawn point to be. Then just move the player to SpawnPoint's transform.position.