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 Sawula · May 30, 2015 at 08:18 PM · gameobjectinstantiatespawn

Spawn items around GameObject

Hello guys, I have a little problem here. I got a GameObject in my scene, which I would like, when clicking a button, to spawn 8 new object (sphere) around it, in each directions. I started by making one, since making the others would just need to change the Vector3 of the instantiate. But it's not working properly, it keep spawning without pause. I need it to spawn just one than stop. I tried a the code found here http://docs.unity3d.com/ScriptReference/Object.Instantiate.html But it doesn't work. So if anyone can help, thanks a lot in advance

 using UnityEngine;
 using System.Collections;
 
 public class virustir : MonoBehaviour {
 
     public GameObject prefab1; //my sphere to instantiate. It's just a sphere gameobject with no rigidbody, no tag, nothing.
     public float distance;
 
         // Use this for initialization
     void Start () {
         distance = 0.6f;
     
     }
     
     // Update is called once per frame
     void Update () {
 
 
         if (clictirvirus.shooting) { //check if the button is on
             for (int i = 0; i < 1; i++) { //attempt to limit spawning to one
                 Instantiate( prefab1, transform.position + ( new Vector3(0,1,0) * distance), transform.rotation );
                     i++;}
             
 
 
         }
         Debug.Log ("Virus tir " + clictirvirus.shooting);
     
     }
 }
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 Johnz1234 · May 30, 2015 at 08:39 PM 0
Share

Where you want to spawn item's and how can u post picture :D

avatar image Sawula · May 30, 2015 at 08:58 PM 0
Share

I want this (just dragged prefabs around to make that figure, but I want it to spawn. The yellow cube is my main gameObject and the green sphere are the items I'm trying to instantiate

The script would be attached to the yellow cube. alt text

bandicam-2015-05-30-22-56-06-881.png (293.4 kB)

3 Replies

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

Answer by awplays49 · May 30, 2015 at 09:30 PM

try something like this

 public GameObject prefab1;
     public GameObject rotator; // attach an empty object that is at the same position as the object with this script
     public float distance;
     public int amountOfObjectsToSpawn;
     public Vector3 rotatorAxis;
     
     // if conditions desired met
     for (int i = 0; i < amountOfObjectsToSpawn; i ++)
     {
          GameObject instance = Instantiate (prefab1, transform.position + Vector3.up * distance, Quaternion.identity) as GameObject;
          instance.transform.parent = rotator.transform;
          rotator.transform.rotation += rotatorAxis * 360 / amountOfObjectsToSpawn;
     }

let me know if all goes well :)

Comment
Add comment · 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
0

Answer by Johnz1234 · May 30, 2015 at 09:08 PM

you can save them all them as prefab and do something like public GameObject Something; public Transform Spawn; OnMouseDown(){ Instantiate(Something,Spawn.position,Spawn.rotation); } add this script to the yellow cube make spawn position just some gameObject where u want them to spawn and gameobject something your thing,player or anything u want to spawn.

Comment
Add comment · 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
0

Answer by Eiyon · May 31, 2015 at 08:24 AM

if you want to stop your for loop just use break;

for (int i = 0; i < 1; i++) { //attempt to limit spawning to one Instantiate( prefab1, transform.position + ( new Vector3(0,1,0) * distance), transform.rotation ); i++; break; // the for loop will start. }

then you can continue to do whatever else you want to do!

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 Graham-Dunnett ♦♦ · May 31, 2015 at 08:25 AM 0
Share

Please read the FAQ and view the tutorial video to learn how to format script code.

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

24 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

Related Questions

Problem with spawn object 0 Answers

How to spawn gameobject from host to all clients 1 Answer

Spawning GameObject contained within another 1 Answer

object not spawning but no error message 2 Answers

How can I set a GameObject variable on an instantiated object? (It always instantiates empty!) 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