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 /
  • Help Room /
avatar image
0
Question by Cirkitype · Feb 15, 2016 at 04:09 PM · destroy objectarray of gameobjects

Im having trouble deleting a single game object from an array of game objects

I have spawner that spawns objects every 2 seconds and when the player cube moves over them I want the object that it moved over to be deleted from the array. I get all sorts of errors though.

  • Code using UnityEngine; using System.Collections; using UnityEngine.UI;

public class Spawner : MonoBehaviour {

 public GameObject obstacle;
 public GameObject player;
 public int score;
 private float nextSpawn;
 private float spawnTime;
 public Text scoring;
 private GameObject obstacleDestroy;
 public GameObject[] obstacles;
 public int count;
 void Start ()
 {
     scoring.text = "Score: ";
     nextSpawn = 2;
 }





 void Update () {
     
     obstacles = GameObject.FindGameObjectsWithTag("Obstacle");
     scoring.text = "Score: " + score; 
     Vector3 position = new Vector3(this.transform.position.x,.1f,this.transform.position.z);
     for( int i = 0; i < obstacles.Length; i++)
     {
     obstacles[i] = obstacleDestroy;
     if (Time.time > spawnTime) 
     {
         
             spawnTime = Time.time + nextSpawn;
             obstacleDestroy = (GameObject)Instantiate(obstacle, position, this.transform.rotation);
             count += 1;
     }

     }

     if(obstacles[count -1] != null)
     {
         if(player.transform.position.x > obstacles[count - count].transform.position.x )
         {
             Destroy(obstacles[count - count + 1]);
             count -= 1;
             score += 1;
         }
     }

 }


}

Comment
Add comment · Show 3
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 meat5000 ♦ · Feb 15, 2016 at 03:15 PM 1
Share

$$anonymous$$eet us half way. Post your errors.

Change your array for a List.

avatar image NoseKills · Feb 15, 2016 at 05:00 PM 1
Share

There are some things that don't make sense to me here.

For example:

 obstacles = GameObject.FindGameObjectsWithTag("Obstacle");
 ...
 for( int i = 0; i < obstacles.Length; i++)
 {
      obstacles[i] = obstacleDestroy;


You get a list of all objects with a certain tag, but before you do anything with those objects you put some other object reference into each index of the array so you lose reference to the objects you just searched for.

Then you for example check

 if(obstacles[count -1] != null)
 {

but since you just put obstacleDestroy to every index, that's what you'll find there.

In the end you destroy some object in the array but since the same object is put into many places, the array might become quite empty

Could you please throw in a comment or a few in the code and/or explain what you want to do and what you think the code does so we get a better idea of what the goal is.

Or maybe i'm just reading this all wrong on my mobile again :)

avatar image Salmjak · Feb 15, 2016 at 10:02 PM 0
Share
 Destroy(obstacles[count - count + 1]);

Sooo... That would be just obstacles[1] then.

And obstacles[count - count].transform.position.x is just obstacles[0].

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you disable OnDestroy() before level is reloaded? 3 Answers

Destroy on collision that actually works PLEASE HELP 0 Answers

GameObject.Destroy(gameObject); Not Working? 0 Answers

2D Object Spawn Lag 1 Answer

Create new transform when other transfrm is destroyes 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