Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by ShinyTaco · Dec 10, 2015 at 07:26 AM · prefabarrayspawnarraystransform.position

How to Spawn a Prefab at an already Spawned Prefab's that's from an array at their transform.position?

I'm trying to spawn prefabs on to other prefabs that have already been spawned from an array.

So, let's say I spawned 10 prefabs from my array.

I now need to spawn a prefab at each and every one of the previously spawned prefabs position. This need to be done one at at a time.

How do I go about doing this?

I'm thinking that getting each spawned elements transform.position would be one way to go about it, but I don't know how to get the transform.position for each spawned prefab's respective element.

Maybe another way is to name each clone uniquely myPrefab1, myPrefab2, etc, etc. and then spawn the second prefab on top of them that way, but again, I do not know how to get each prefabs transform.position.

Here's my code to give you an idea of what I'm trying to do:

         if (Input.GetMouseButtonUp(0)){
             if (i<whatEverYouFeelLike){
                 stuffFromArray = Instantiate(stuffArray[i], spawnPosition, transform.rotation) as GameObject;
                 stuffFromArray.name = "stuffFromArray " +i.ToString();
                 i++;
                                 }
             }
 
         if (Input.GetMouseButtonUp(0) && spawnSecondRound){
                      // Spawn Second Round of Stuff Here
 
                     // Spawn Second Round of Stuff at the stuffFromArray.transform.position, separately and one at a time, each time the mouse button is clicked
 
                    //  How?
 }

Any help is really appreciated.

Thanks!

Comment
Add comment · Show 1
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 ShinyTaco · Dec 11, 2015 at 12:47 AM 0
Share
 GameObject myPrefabSphere = Instantiate (prefabSphere, stuffFromArray[i].transform.position, transform.rotation) as GameObject;


I'm trying to get

stuffFromArray[i].transform.position

to move on to the second i in the array to spawn another sphere.

Not sure how to do that.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mr_Edward · Dec 10, 2015 at 08:59 AM

I'm not quite sure I completely understand what it is that you wan't to do.

Anyway: You can get the first prefabs transform quite simply by calling

 stuffFromArray = Instantiate(stuffArray[i], spawnPosition, transform.rotation) as GameObject;
 
 debug.log("Prefab position: " + stuffFromArray.transform.ToString());


I'll modify your script to do what I think you want it to do, but then again I'm not quite sure I understood you correctly:

 if (Input.GetMouseButtonUp(0))
         {
 
 //(I just added these so I didn't get any compiler errors)
 //---------------------------------------------------------------------------------
             int i = 10;
             int whatEverYouFeelLike = 1;
             GameObject[] stuffArray = new GameObject[5];
             GameObject[] SecondSpawnstuffArray = new GameObject[5];
             Vector3 spawnPosition = transform.position;
 //---------------------------------------------------------------------------------
 
             if (i < whatEverYouFeelLike)
             {
                 //You don't have to save "stuffFromArray" for any longer than this frame, and you only use it in this if statement. Therefor you can define it here, by putting "Gameobject" in front of it.
                 GameObject stuffFromArray = Instantiate(stuffArray[i], spawnPosition, transform.rotation) as GameObject;
                 stuffFromArray.name = "stuffFromArray " + i.ToString();
 
                 GameObject SecondSpawnStuffArray = Instantiate(SecondSpawnstuffArray[i], stuffFromArray.transform.position, transform.rotation) as GameObject;
                 SecondSpawnStuffArray.name = "SecondSpawnstuffFromArray " + i.ToString();
                 i++;
             }
         }

     //You don't actually need this, atleast not for what you have presented on the forum. If you need it for some other reason, you can simply save the transform to a private variable, and use it here.
     /*
     if (Input.GetMouseButtonUp(0) && spawnSecondRound)
     {
         // Spawn Second Round of Stuff Here

         // Spawn Second Round of Stuff at the stuffFromArray.transform.position, separately and one at a time, each time the mouse button is clicked

         //  How?
     }*/
Comment
Add comment · Show 1 · 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 ShinyTaco · Dec 10, 2015 at 11:38 PM 0
Share

So, what I'm trying to do is spawn some prefabs from an array for the example lets just say they're squares.

Every time the mouse button is clicked a new square spawned from the array is moving. Let's say there's 10 of them for the example.

Now, the part where I'm stuck is I need to spawn a sphere above each of the already spawned squares.

Each time the mouse button is clicked a sphere is to spawn above each of the squares, 1 sphere at a time, each time the mouse button is clicked.

Preferably this needs to be done in the same sequence as the order from which the squares were spawned but this isn't totally necessary.

Here's some code that might help to understand better:

 GameObject myPrefabSphere = Instantiate (prefabSphere, stuffFromArray.transform.position, transform.rotation) as GameObject;

That code will spawn the spheres all at once when the mouse is clicked. I need for the spheres to spawn only one at a time.

Hope that helps to clarify.

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

34 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 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

Moving spawned enemies randomly towards several pre-defined positions 1 Answer

Prefab spawner doesnt work 1 Answer

how to not repeat random array 1 Answer

Spawn array error 2 Answers

Spawn prefabs from array in specific order given the size of prefab 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