- Home /
How to instantly move a newly instantiated prefab?
I am trying to write a class that randomly picks between four prefabs from a Resource Folder, instantiates the randomly picked prefab, and places said prefab in a random location. I have the first two parts working, but I can't seem to get the prefab placed where I want it. Instead, it always instantiates where I had the object when I made it into a prefab. What am I doing wrong?
Here's the code I'm using to try to place the prefab. xCoord and yCoord are the x and y coordinates that I want the prefab to be at, and obstacle refers to the prefab itself.
xDiff = xCoord - obstacle.transform.position.x;
yDiff = yCoord - obstacle.transform.position.y;
obstacle.transform.Translate(xDiff, yDiff, 0f);
Answer by Joyrider · Aug 13, 2013 at 04:48 PM
Why aren't you using the instantiate with positioning information?
static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
and just input the wanted position.
I'm guessing he's using PrefabUtility.InstantiatePrefab which doesn't have that option
Can you do that with a Resource Folder object? I'm still new to Unity, so I don't know whether that's possible or not.
Also, my instantiation code prior to trying the position information is: obstacle = Instantiate(Resources.Load("obstacle_temp")) as GameObject;
I just replace "obstacle_temp" with the name of the prefab I want.
As far as trying the positioning information, I'm going to do that. However, I want the rotation to stay the same as it was when I created the prefab. I just want the position to change. Is that possible?
@Dave, PrefabUtility is an editor class, so didn't think he would be using that ;)
@CrossRiverGames, yes you can do that. The Resources.Load("name") just replaces the original object entry.
As for the orientation, I guess you could first call your load sequence, put the reference in a variable and use
Instantiate(prefabReference,newPosition,prefabReference.transform.orientation);
but I'm not sure on that one.
@Joyrider I'm confused about what you're trying to say. Could you please clarify? I have the code posted in my comment below.
Well, have you tried something like this:
// Getting the prefab from the resources
GameObject myPrefab = Resources.Load("obstacle_temp") as GameObject;
// instantiating your prefab in the scene, with new position but original orientation (checked it, it should work).
obstacle = Instantiate(myPrefab,new Vector3(xCoord,yCoord,0),myPrefab.rotation) as GameObject;
Answer by DaveA · Aug 13, 2013 at 04:46 PM
obstacle.transform.position = new Vector3(xCoord, yCoord, 0);
Unfortunately, that didn't work. The prefabs instantiated in the exact same spot that the original objects had been when the prefabs were created.
Okay I get it... @CrossRiverGames, you can't set the position of a prefab as an asset
An asset technically has no existence in space, and thus cannot be positioned or oriented.
You need to instantiate the prefab, and then chance the instantiated object's position ;)
All right, here's the full method (without the positioning information in the instantiation because I haven't figured out how to keep the rotation the same):
void Spawn(int type, float xCoord, float yCoord)
{
GameObject obstacle; // Create a space in memory for the obstacle
switch(type)
{
case 0:
obstacle = Instantiate(Resources.Load("obstacle_temp")) as GameObject; // The obstacle names are temporary
break;
// Cases 1, 2, and 3 are the same as case 0, only with different obstacle names
default: // $$anonymous$$; don't spawn an obstacle
obstacle = new GameObject(); // This line is only to prevent compiler error; it will never be executed.
break;
}
obstacle.transform.position = new Vector3(xCoord, yCoord, 0f);
}
Your answer
Follow this Question
Related Questions
Instantiate a prefab results in a new prefab in a different location than expected 2 Answers
Why can't I instantiate an object that is +5 in the x and z axis? 1 Answer
Need Help With 2D Array Grid Position Tracking With JavaScript 1 Answer
Instantiate a random prefab at an objects location 3 Answers