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 Subliminalman · Jun 27, 2012 at 06:04 AM · instantiatenullarraylist

Adding Instantiated Objects to ArrayList but it returns null C#

Hello, I have an obstacles manager that keeps track of all obstacles in the scene, instantiating them and then supposed to destroy them. In order to do this I wanted to keep references to the Instantiated objects in an ArrayList (or List but I'm having the same issue in both).

When I add the Instantiated object to the ArrayList the index count goes up but when I try to access the index it returns null. I can see the objects in the scene so I don't understand the problem.

Here is my code, I commented out one section but thats another route I was taking to see if it would solve the issue. Any help would be great.

 public int maxObstacles, minObstacles;
     public float minCreateTime, maxCreateTime;//Randomize time to create objects between these 2 values
     private float createTime, currentTime; //Random amount of time between the creation of obstacles
     private Vector3 obstacleBounds;//The position for which to remove the obstacle from the scene list
     public Transform cameraTransform;
     public GameObject[] obstacleTypes;
     private Transform tempObstacle;
     private ArrayList obstaclesInSceneList;//Obstacles currently in the scene
 
 
 // Use this for initialization
 void Start () 
 {
     obstaclesInSceneList = new ArrayList();
     createTime = Random.Range(minCreateTime, maxCreateTime);
     currentTime = 0;
 }
 
 // Update is called once per frame
 void Update () 
 {
     
     if(obstaclesInSceneList.Count <= minObstacles && obstaclesInSceneList.Count <= maxObstacles)
     {
         if(currentTime >= createTime)
         {
             createTime = Random.Range(minCreateTime, maxCreateTime);
             currentTime = 0;
             

             tempObstacle = Instantiate(obstacleTypes[Random.Range(0, obstacleTypes.GetLength(0) - 1)],
                 new Vector3(Random.Range(-(Screen.width /2), (Screen.width /2)),cameraTransform.position.y - Screen.height, 0),
                 transform.rotation) as Transform;
             Debug.Log("Temp is null" + tempObstacle == null);
             
             obstaclesInSceneList.Add(tempObstacle);
 
             /*obstaclesInSceneList.Add(Instantiate(obstacleTypes[Random.Range(0, obstacleTypes.GetLength(0) - 1)],
                 new Vector3(Random.Range(-(Screen.width /2), (Screen.width /2)),cameraTransform.position.y - Screen.height, 0),
                 transform.rotation) as Transform);*/
         }
     }

                                 //Check to see if any of the obstacles are off screen above the camera
 
     obstacleBounds = new Vector3(0, cameraTransform.position.y + Screen.height, 0); 
     
     Debug.Log(obstaclesInSceneList.Count);
     try
     {
         for(int i = 0; i < obstaclesInSceneList.Count; i++)
         {
             
             if(obstaclesInSceneList[i].position.y > obstacleBounds.y)
             {
                 
                 obstaclesInSceneList.Remove(obstaclesInSceneList[i]);
                 
             }
         }    
     }
     catch
     {
         Debug.Log("Something is null");    
     }
     currentTime +=1;
         
 }
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

3 Replies

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

Answer by Bunny83 · Jun 28, 2012 at 03:06 AM

You stepped into the as-cast trap....

You declared your obstacleTypes array as GameObject array. That's not a problem, but when you instantiate you try to cast the instantiated object to Transform which of course fails since a GameObject isn't a Transform. Because you used the as-cast you don't get any casting error. The cast just returns null.

Btw. Why do you use an ArrayList? It's slow and requires casting since it's not strongly typed. Use a generic List<> instead. First you should decide if you want to store GameObject references or Transform references and adjust your obstacleTypes array and the cast.

The generic List requires the System.Collections.Generic namespace.

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 Subliminalman · Jun 28, 2012 at 03:17 AM 0
Share

I was using that earlier but I read that iOS does not support the List(). I would like this clarified since I would like to keep all of this cleaner. Currently I do have the items going into the List correctly without being null, now I have the problem of them not being destroyed. I show the code I used below.

avatar image Bunny83 · Jun 28, 2012 at 04:50 AM 0
Share

Again the same mistake. You store Transforms in the array. A Transform can't be an Obstacle. It seems you are a bit confused about your types. If all of your prefabs have an Obstacle script attached, you might want to use this type ins$$anonymous$$d of Transform or GameObject. You can also instantiate the prefab if you provide a reference to any of the attached components.

So you should:

  • change obstacleTypes to public Obstacle[] obstacleTypes;

  • You have to re-assign your prefabs when you change the type of the array. You can of course only assign prefabs that have such a script attached.

  • Your tempObstacle also has to be an Obstacle now and your cast should of course cast to that type.

  • Furthermore tempObstacle should be a local variable and not a member variable.

  • When you destroy them, you don't need to test the reference if it's an Obstacle since all references are Obstacle or null, so testing for null would make sense. If it's null you can of course remove it from the list.

  • Next thing is, when you call Destroy with a reference to an Obstacle component, you will just destroy the component, not the whole gameobject it is attached to. If you want to destroy the whole GameObject, you have to use: Destroy(obstaclesInSceneList[j].gameObject);. This of course only works with a strongly typed List which is now supported on all platforms afaik.

avatar image Subliminalman · Jun 28, 2012 at 05:31 AM 1
Share

AH the last thing you said worked! I didn't put up all of my code but I did all of that before hand but was they still weren't deleting. The Destroy(obstaclesInSceneList[j].gameObject); works though! Thank you :D

avatar image
0

Answer by DaveA · Jun 27, 2012 at 06:50 AM

Problem in removing? 'Remove' will change the Count, so the indexes will be off. Use 'foreach' instead of 'for'

Comment
Add comment · Show 5 · 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 Subliminalman · Jun 27, 2012 at 07:06 AM 0
Share

Well it's even before that. Right after I instantiate and add to the ArrayList I could do something like I did with Debug.Log("Temp is null" + tempObstacle == null); and it would return true stating that index is null.

avatar image DaveA · Jun 27, 2012 at 09:07 PM 0
Share

If Instantiate returns null, then something went wrong there. Did you assign GameObjects to all the entries in obstacleTypes, like with the Inspector?

avatar image Subliminalman · Jun 28, 2012 at 02:58 AM 0
Share

So I now have it actually assigning the indexes but now I can't destroy the object in the scene. I even tried a test where it would change the position of the obstacle to where the camera is so I know it is accessing the object. Here's what I'm now doing in the destroy area

obstacleBounds = new Vector3(0, cameraTransform.position.y + Screen.height, 0);

     try
     {
         foreach(Obstacle obs in obstaclesInSceneList)
         {
             if(obs.transform.position.y > obstacleBounds.y)
             {
                 obs.isActive = false;    
                                                                                     //Test code 
                                                                                     //obs.transform.position = cameraTransform.position
                                        //Destroy (obs); //Creates an error since it can't 
                                                                                    //access the next object in the arraylist
             }
             
         }
         
         for(int j = 0; j < obstaclesInSceneList.Count; j++)
         {
             if(obstaclesInSceneList[j] is Obstacle)
             {
                 Obstacle tempObstacle = (Obstacle)obstaclesInSceneList[j];
                 if(!tempObstacle.isActive)
                 {
                 Destroy((Obstacle)obstaclesInSceneList[j]);
                     
                 obstaclesInSceneList.RemoveAt(j);    
                                Debug.Log ("Removed: " + j);
                                Destroy (tempObstacle);
                     
                 Debug.Log("Destroyed: " + j);
                 }
             }
         }
         
     
     
     }
     catch($$anonymous$$issingReferenceException e)
     {
         Debug.Log(e.ToString());    
     }



I don't understand why it won't destroy the obstacle

avatar image Bunny83 · Jun 28, 2012 at 03:15 AM 0
Share

btw, foreach doesn't work either. The enumerator of the ArrayList or List class will check the version of the List which is incremented if you change anything, even when you assign a value to an existing element. Generally there are two ways to iterate through a list when you want to remove elements:

  • Use a while loop and increment the count variable only when you didn't remove an element this iteration.

  • Count backwards. That's the easiest way, but you have to be careful with the bounds.

    for(int j = obstaclesInSceneList.Count-1; j >= 0; j--) {

avatar image Subliminalman · Jun 28, 2012 at 03:38 AM 0
Share

I'm trying the backwards for loop but still nothing. Here's the current code for it.

obstacleBounds = new Vector3(0, cameraTransform.position.y + Screen.height, 0);

     try
     {
         for(int j = obstaclesInSceneList.Count-1; j >= 0; j--)
         {
             if(obstaclesInSceneList[j] is Obstacle)
             {
                 if(((Obstacle)obstaclesInSceneList[j]).transform.position.y > obstacleBounds.y)
                 {
                     Destroy((Obstacle)obstaclesInSceneList[j]);
                     obstaclesInSceneList.RemoveAt(j);
                     continue;
                 }
                 
             }
         }    
     }
     catch($$anonymous$$issingReferenceException e)
     {
         Debug.Log(e.ToString());    
     }
avatar image
0

Answer by ramp · Jan 08, 2013 at 12:37 PM

1.create ArrayList for numbers of obstacles that's you want instantiate . 2.Use a Switch case for different position .

var obstacles : GameObject[];

private var delta_time : int;

private var stopTime : float = 0;

private var scene_load_time : float = 0.0; private var loop_counter : int = 0;

private var caseCalled : int = 0;

var pos1 : Vector3; var pos2 : Vector3; var pos3 : Vector3; var pos4 : Vector3;

function Start () { CaseCalled(); }

function Update () { if(stopTime > delta_time) { var temp_time : int = scene_load_time/30; if(temp_time == loop_counter) { loop_counter += 1; if(loop_counter > 6) { loop_counter = 6; } }

 for(var i =0; i< loop_counter ; i++)
 {
 CaseCalled();
 }
 stopTime = 0;
 }
 stopTime += Time.deltaTime;
 scene_load_time += Time.deltaTime;

}

pos1 = Vector3(Random.Range(-500,500),400,0);
pos2 = Vector3(-500,Random.Range(-360,400),0);
pos3 = Vector3(Random.Range(-500,500),-360,0);
pos4 = Vector3(500,Random.Range(-360,400),0);

 switch(caseCalled)
 {
     case 0 :  
             Instantiate(obstacles[Random.Range(0,7)],pos4,Quaternion.EulerAngles(-89.5,0,0));
             break;
     case 1 : 
             Instantiate(obstacles[Random.Range(0,7)],pos1,Quaternion.EulerAngles(-89.5,0,0));
             break;
     case 2 : 
             Instantiate(obstacles[Random.Range(0,7)],pos2,Quaternion.EulerAngles(-89.5,0,0));
             break;
     case 3 : 
             Instantiate(obstacles[Random.Range(0,7)],pos3,Quaternion.EulerAngles(-89.5,0,0));
             break;
     default : 
             Instantiate(obstacles[Random.Range(0,7)],pos3,Quaternion.EulerAngles(-89.5,0,0));
 }

}

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

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

7 People are following this question.

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

Related Questions

getting GameObject from Array and then instantiating 2 Answers

prefab is null when I try to instantiate from another script 1 Answer

Missing objects From Build 1 Answer

Getting NullReference when calling Instantiate from another script 0 Answers

Game Object seems empty after instantiation 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