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 CrossRiverGames · Aug 13, 2013 at 04:41 PM · instantiateprefabpositionlocationinstantiation

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);
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 7 · 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 DaveA · Aug 13, 2013 at 04:52 PM 0
Share

I'm guessing he's using PrefabUtility.InstantiatePrefab which doesn't have that option

avatar image CrossRiverGames · Aug 13, 2013 at 04:53 PM 0
Share

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?

avatar image Joyrider · Aug 13, 2013 at 05:32 PM 0
Share

@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.

avatar image CrossRiverGames · Aug 15, 2013 at 07:01 PM 0
Share

@Joyrider I'm confused about what you're trying to say. Could you please clarify? I have the code posted in my comment below.

avatar image Joyrider · Aug 16, 2013 at 01:15 AM 0
Share

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;
Show more comments
avatar image
0

Answer by DaveA · Aug 13, 2013 at 04:46 PM

 obstacle.transform.position = new Vector3(xCoord, yCoord, 0);
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 CrossRiverGames · Aug 13, 2013 at 04:56 PM 0
Share

Unfortunately, that didn't work. The prefabs instantiated in the exact same spot that the original objects had been when the prefabs were created.

avatar image Joyrider · Aug 13, 2013 at 05:25 PM 1
Share

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 ;)

avatar image CrossRiverGames · Aug 13, 2013 at 05:44 PM 0
Share

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

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

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

GameObject change Position after game started 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