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 rodricksryan10 · Jul 16, 2019 at 06:37 AM · 2d game2d-platformer2d-physics2d array

Error while spawning multiple objects

So I am new to Unity and am trying to spawn multiple objects from bottom going up.I have created a startpoint object to destroy the object if it enters and detect multiple objects by its tag and endpoint to spawn my 2d objects from there. Also the first two objects randomly and then stop spawning.This is my code....thank you.

//start using System.Collections; using System.Collections.Generic; using UnityEngine;

public class controller : MonoBehaviour { public GameObject obs1; public GameObject obs2; public GameObject obs3; public GameObject obs4; public GameObject obs5; public GameObject obs6; public GameObject obs7; public GameObject startpost;

 int counter = 0;
 // Start is called before the first frame update
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {

 }
 private void OnTriggerEnter2D(Collider2D collision)
 {
     counter = Random.Range(1, 8);
     if (collision.gameObject.tag == "obstacle")
     {
         if (counter == 1)
         {
             Instantiate(obs1, new Vector2(startpost.transform.position.x, obs1.transform.position.y), Quaternion.identity);
         }
         else
              if (counter == 2)
         {
             Instantiate(obs2, new Vector2(startpost.transform.position.x, obs2.transform.position.y), Quaternion.identity);
         }
         else
              if (counter == 3)
         {
             Instantiate(obs3, new Vector2(startpost.transform.position.x, obs3.transform.position.y), Quaternion.identity);
         }
         else
              if (counter == 4)
         {
             Instantiate(obs4, new Vector2(startpost.transform.position.x, obs4.transform.position.y), Quaternion.identity);
         }
         else
              if (counter == 5)
         {
             Instantiate(obs5, new Vector2(startpost.transform.position.x, obs5.transform.position.y), Quaternion.identity);
         }
         else 
         if (counter == 6)
         {
             Instantiate(obs6, new Vector2(startpost.transform.position.x, obs6.transform.position.y), Quaternion.identity);
         }
         else
              if (counter == 7)
         {
             Instantiate(obs7, new Vector2(startpost.transform.position.x, obs7.transform.position.y), Quaternion.identity);
         }
         Destroy(collision.gameObject);
     }
 }

} //end

//Error MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. controller.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/controller.cs:50)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by pyramidhead01 · Jul 16, 2019 at 01:20 PM

What you want to do is pool your objects, and then set them active and inactive.

The solution I'm giving you isn't tested, I pass the time at work with this, so if you run into issues, let me know.

 using System;
 using Unity.Engine.UI;
 using System.Collections.Generic;
                 
 public class Controller : MonoBehaviour 
 {
     //Instead of making a bunch of GameObject variables, create a list, and set it publicly in inspector
     public List<GameObject> objects = new List<GameObject>();
     int counter;
     bool startSpawning;
     public GameObject startpost;
     
     private void Start()
     {
         foreach(GameObject obj in objects)
         {
             Instantiate(obj, transform.position, Quaternion.Identity);
             obj.SetActive(false);
         }
     }
     
     private void Update()
     {
         if(startSpawning == true)
         {
             Spawn();
         }
     }
 
 
      private void OnTriggerEnter2D(Collider2D collision)
      {
          if (collision.gameObject.tag == "obstacle")
         {
             startSpawning = true;
          }
     }
     
     private void Spawn()
     {
         counter = Random.Range(0, objects.Count);
         objects[counter].SetActive(true);
         objects[counter].transform.position = (startpost.transform.position.x, objects[counter].transform.position.y);
         objects.Remove(counter);
         if(objects.Count < 1)
         {
             Destroy(collision.gameObject);
             startSpawning = false;
         }
     }
 }



The above solution you have to add all the game objects to the list publicly in the inspector. This should also spawn all the objects in a random order, I'm not 100% sure if that works (or works the way you want) but it should spawn everything before it destroys the colliding object in a much more elegant way than you had before.


For more seasoned coders, yes I'm aware a while loop (or maybe even for loop) would also work, but just trying to keep this solution simple.


Again, if it doesn't work, let me know and I'll try again; it also is a good idea not to make sure I made spelling mistakes or something like that first because I don't have a compiler, have not tested this, and I might have a typo in there that is causing an error, so please make sure everything that is underlined red on your end isn't a typo from me.

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 rodricksryan10 · Jul 17, 2019 at 04:21 PM 0
Share

Thank you very much for that long code! Sorry to disturb you but I am getting these two errors around line 44 and 45 tried my best couldn't solve them...

Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type '(float x, float y)' to 'UnityEngine.Vector3' Assembly-CSharp 44 Active

Error CS1503 Argument 1: cannot convert from 'int' to 'UnityEngine.GameObject' Assembly-CSharp 45 Active

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

145 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

Related Questions

I need to shoot a projectile in my 2D platformer in the opposite direction my player is dashing which can be in any direction the player points the joystick 2 Answers

Having some stuck issues on the 2D infinite runner 0 Answers

Way to achieve 3 lane mechanism in a 2D game 0 Answers

HOLD JUMP BUTTON TO JUMP HIGHER 2 Answers

Problem with 2D movment system C#. Keeps moving when no command is given 0 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