Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 everything 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 hits 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 this I just had the one which I tried to instantiate twice).

What am I missing? why does this 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 this for initialization
 void Start () {
     if (sizeKey < 1)
     {
         Destroy(this.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(this.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 which subsequent objects will be created and that may have unexpected results.

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

 //Instead of having this:
 GameObject tempAsteroid = asteroidPrefab;
 //bla bla
 Instantiate(tempAsteroid);
 //You should have this:
 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 this. 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 think 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, this 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 hierarchy 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 this 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 thingos here*/);

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

Ohhhhhh! I see!

public GameObject asteroidPrefab, asteroidPrefab2;

Yah wrote this, but never assigned it to any value! Hah, I see how that would make things 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 nothing is created! The asteroid prefab you want to use isn't a game object, it's this... data-thing, which you feed into the instantiate method to create a game object with the properties of the prefab. ( I think). 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

@$$anonymous$$axDx 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.

Follow this Question

Answers Answers and Comments

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

Related Questions

Create random order and go through till the end (C#) 1 Answer

Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers

Is there anyway to delete clones that are local variables? 1 Answer

How to make TCG Deck (Instatiate based on Prefab) 1 Answer

How to instantiate platforms like Doodle Jump game ? 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