Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SirBoboHobo · Apr 06, 2015 at 12:27 PM · c#instantiateprefabspawn

instatiates at a bad position

I have 10 prefabs that are instantiated by a cube with a the following script attached to it:

     public GameObject[] obj;
     public float spawn = 2f;
 
     // Use this for initialization
     void Start () {
         Spawn ();
     }
     void Update(){
         transform.Translate (Vector2.up * Time.deltaTime);
     }
     public void Spawn(){
             Instantiate (obj [Random.Range(0, obj.GetLength(0))],transform.position, Quaternion.identity);
         Invoke ("Spawn", spawn);
     }
 }
 

the problem is that some prefabs get instantiated at a bigger x, like to the side, some with a negative x which spawns them to the other side causing only part of them to be seen and other is out of bounds, how do i position them all on same x?

EDIT: all of the prefabs are instantiated at x = 0, but each prefab is to be centered has different x, such as 1, 0.5, -6.3...

Comment
Add comment · Show 2
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 moxiewhimsy · Apr 06, 2015 at 03:50 PM 0
Share

Do they spawn together, within the same period of time?

avatar image SirBoboHobo · Apr 06, 2015 at 08:24 PM 0
Share

no, over time, every spawn seconds, as you see in the code, spawn = 2, so each 2 second a prefab is spawned. the cube goes up more and more and more, endlessly, so it spawns prefabs on the y axis

1 Reply

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

Answer by ethestel · Apr 06, 2015 at 03:50 PM

Hello,

Instantiate method relies on position of its game objects transform, which you change by calling transfrom.Translate on every frame update.

You can assign a constant Vector3 to the newly Instantiated objects position

 //this will spawn all objects at 0,0,0 world coordinates
 Instantiate (obj [Random.Range(0, obj.GetLength(0))],new Vector3(0,0,0), Quaternion.identity);

Comment
Add comment · Show 5 · 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 SirBoboHobo · Apr 06, 2015 at 04:26 PM 0
Share

my cube always goes up and up and up, i need it to spawn at the position the cube is, but i need the x to be 0, Vector3(0,0,0) just spawns all prefabs at same place.

avatar image ethestel · Apr 07, 2015 at 07:14 AM 0
Share

The Vector3 variable you're passing to that method specifies newly instantiated objects position. You can change Vector3's parameters to spawn the object wherever you want to.

  Instantiate (obj [Random.Range(0, obj.GetLength(0))],new Vector3(0,transform.position.y,transform.position.z), Quaternion.identity);
 

This will spawn that object on the same world coordinates with the game object this script is attached on.

avatar image SirBoboHobo · Apr 07, 2015 at 12:05 PM 0
Share

It instantiates at 0, some prefabs need like -6.3 to be on x=0, how can i access the transform.position.x of the prefab, each prefab has an x that i specified in order for it to be in the center, like 1.1, 6.3, and so on, how to specify that the place spawned is Vector2(x of the prefab, transform.position.y)

avatar image ethestel · Apr 07, 2015 at 02:52 PM 0
Share

It instantiates at 0, some prefabs need like -6.3 to be on x=0

I think you're talking about the difference between position and localPosition, but there's no way I can be certain about it. Can you supply your project files?

avatar image SirBoboHobo · Apr 07, 2015 at 03:27 PM 0
Share

I was building the prefabs in a different scene so i dont interrupt the main scene, so some aren't really in the middle, so they will be in the middle it needs to be like x=-6.3

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

21 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 avatar image avatar image avatar image avatar image

Related Questions

Don't allow prefab to spawn if prefab already exists at the spawn location. 1 Answer

Why is Prefab declared as type Transform? 2 Answers

Why does the instantiation of a prefab work well when the scene is first loaded, but then the cloned GameObjects are no longer displayed in the scene? 0 Answers

How do I instantiate certain objects to appear in a specific spot? 3 Answers

Spawning a prefab at another object's location 3 Answers


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