• 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 megabrobro · Nov 10, 2017 at 10:03 PM · c#unity 5instantiateinstantiation

How can I Instantiate two new GameObjects at the same time? (Attempting Asteroids style game)

Hi all,

I haven't been at my computer learning code or game dev in a few months and am just starting again. I am trying to make a game very similar to Asteroids. In 3d but fixed camera from above so basically disregarding the Y Axis at the moment.

I have everyt$$anonymous$$ng working fine so far such as the rocket stays in middle and can rotate, and fire weapon. The Asteroids for now all come from the top side of the screen, and have some code to randomise the X location and speeds.

When the laser $$anonymous$$ts the Asteroid I want it to destroy that Asteroid and create two more that are half the size and with a Rotation added of 45/-45 degrees.

I have the code that I thought would work but it is only creating one more and not two. (note: as you'll see I have tried making tempAsteroid2 and even asteroidPrefab2, before t$$anonymous$$s I just had the one w$$anonymous$$ch I tried to instantiate twice).

What am I missing? why does t$$anonymous$$s code not make two more appear upon laser collision??

Any help massively appreciated as always. Many thanks:

 public class Asteroid : MonoBehaviour {
 float speed;
 public GameObject asteroidPrefab, asteroidPrefab2;
 public int sizeKey = 2;
 // Use t$$anonymous$$s for initialization
 void Start () {
     if (sizeKey < 1)
     {
         Destroy(t$$anonymous$$s.gameObject);
     }
     transform.position.Set(transform.position.x, 0f, transform.position.y);
     speed = Random.Range(0.5f, 2f);
 }
 
 // Update is called once per frame
 void Update () {
     GetComponent<Rigidbody>().transform.Translate(Vector3.forward * speed * Time.deltaTime);
 }
 
 private void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Laser")
     {
         BreakAsteroidInHalf();
         Destroy(other.gameObject);
     }
 }
 
 void BreakAsteroidInHalf()
 {
     GameObject tempAsteroid = asteroidPrefab;
     tempAsteroid.transform.position = transform.position;
     tempAsteroid.transform.Rotate(0, -45, 0);
     tempAsteroid.transform.localScale *= 0.5f;
     tempAsteroid.GetComponent<Asteroid>().sizeKey -= 1;
     Instantiate(tempAsteroid);
     GameObject tempAsteroid2 = asteroidPrefab2;
     tempAsteroid2.transform.position = transform.position;
     tempAsteroid2.transform.Rotate(0, 45, 0);
     tempAsteroid2.transform.localScale *= 0.5f;
     tempAsteroid2.GetComponent<Asteroid>().sizeKey -= 1;
     Instantiate(tempAsteroid2);
     Destroy(t$$anonymous$$s.gameObject);
     Debug.Log("Asteroid destroyed and replaced by 2 more");
 
 }
 

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
1
Best Answer

Answer by MacDx · Nov 11, 2017 at 01:13 AM

I see 2 issues with your code.

First of all, you are modifying the prefab. You shouldn't be doing that when instantiating objects, since you are basically changing the template from w$$anonymous$$ch subsequent objects will be created and that may have unexpected results.

What you need to do to solve t$$anonymous$$s is very simple just combine the Instantiate(tempAsteroid); with the GameObject tempAsteroid line.

 //Instead of having t$$anonymous$$s:
 GameObject tempAsteroid = asteroidPrefab;
 //bla bla
 Instantiate(tempAsteroid);
 //You should have t$$anonymous$$s:
 GameObject tempAsteroid = Instantiate(asteroidPrefab);
 //bla bla
 //And so you would do the same for tempAsteroid2
 //As a side note, you shouldn't be needing two prefabs for t$$anonymous$$s. One should be enough since you're only changing a rotation, but, if it is simpler for you then leave it like that

The second issue (and the one that I t$$anonymous$$nk is mainly causing your problem) is that you are placing both the newly instantiated objects in the exact same position and with the exact same scale, t$$anonymous$$s might be making it so you can't see one of them since the other one is on top of it. Have you checked the $$anonymous$$erarchy of objects to see if there's really just one object? Try placing them in different positions, with different scales maybe just to be sure.

Hope t$$anonymous$$s helps!

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 megabrobro · Nov 11, 2017 at 02:37 AM 0
Share

Hi there, thanks for the correct line, I checked some of my old projects (when I understood Unity a bit better) and they have it like that also.

One thing, you have mentioned the second issue about the two items being in the same position etc. We'll I did realise that and it was intended. the colliders are set to be just triggers, and they are rotated differently so i figured they would soon separate.

Also very wierdly, in the Heirarchy inside the editor whislt the game is running, I only see the one instance of the asteroid being created.

I'm very tired now so that might have something to do with why im struggling so much, so I'll write back tomorrow morning after ive had another look and if I still cant figure it out.

Thanks again for your help.

avatar image
0

Answer by Leonstar0 · Nov 11, 2017 at 01:25 AM

EDIT: Oh, I am dead wrong, see MacDx's comment.


Hmmmm...

GameObject tempAsteriod = Instantiate(/*insert t$$anonymous$$ngos here*/);

Maybe? An asteroid prefab probably isn't a game object.

Ohhhhhh! I see!

public GameObject asteroidPrefab, asteroidPrefab2;

Yah wrote t$$anonymous$$s, but never assigned it to any value! Hah, I see how that would make t$$anonymous$$ngs even more confusing! Basically you made empty game objects in your code asteriodPrefab, asteriodPrefab2, then later on created tempAsteriod1 and tempAsteriod2, assigning their reference to the empty game objects. Thus... of course not$$anonymous$$ng is created! The asteroid prefab you want to use isn't a game object, it's t$$anonymous$$s... data-t$$anonymous$$ng, w$$anonymous$$ch you feed into the instantiate method to create a game object with the properties of the prefab. ( I t$$anonymous$$nk). Does that make sense?

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 MacDx · Nov 11, 2017 at 02:08 AM 0
Share

Ammmm

Thus... of course nothing is created!

No, because he said

I have the code that I thought would work but it is only creating one more and not two.

And also no, to this:

Yah wrote this, but never assigned it to any value! Hah, I see how that would make things even more confusing!

Because he probably assigned the variable through the inspector. And if he didn't he would be getting a null reference error but he isn't (or at least he didn't say so).

So I'm pretty sure you are dead wrong here @Leonstar0

avatar image Leonstar0 · Nov 11, 2017 at 02:22 AM 0
Share

@MaxDx Doh! Forgot that public exposed it in the inspector! Yeppers! Yikes! Well... I discovered a wrong answer! Should I leave it there with an edit to say I'm wrong, or should I delete the answer?

avatar image megabrobro · Nov 11, 2017 at 02:34 AM 0
Share

thank you for the attempt of help regardless. And I now understand how to fix this (i beleive) thanks to the above answer. I'll have a go in the morning and see how I get on . Thanks

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

How do i Instantiate gameObjects in between multiple points? 2 Answers

Distribute terrain in zones 3 Answers

How to Spawn after checking if the clones are destroyed. 1 Answer

How do i Instantiate Objects to a Empty GameObject as a Child?? 3 Answers

Instantiated object not showing in scene or hierarchy 2 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