Search Unity

List showing 'None (GameObject)' when adding instantiated Gameobjects

Discussion in 'Scripting' started by nolim, Aug 28, 2014.

  1. nolim

    nolim

    Joined:
    Mar 28, 2014
    Posts:
    22
    I'm writing a space game and maintaining a list of enemies (gameobjects) called 'Enemies'. However when i try and add a new gameobject to the list using:
    Enemies.Add(Instantiate(ShipToSpawn,location, rotation)as GameObject);
    The Enemies list always shows 'none(GameObject)'. The enemy ship appears in the scene at the correct position but clearly isn't being added to the list properly. Anyone know what i'm doing wrong?
     
  2. BobBobson108

    BobBobson108

    Joined:
    Mar 13, 2008
    Posts:
    57
    I had this problem before. Try saving off the thing you want to add as a variable first.

    GameObject ship = Instantiate(ShipToSpawn,location, rotation) as GameObject;
    Enemies.Add(ship);
     
    nolim likes this.
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    If ShipToSpawn isn't declared as a GameObject then your "as GameObject" cast will return null and you'll add null to the List. This will instantiate the thing correctly and add null to your list which will draw in the Inspector as None
     
    nolim likes this.
  4. nolim

    nolim

    Joined:
    Mar 28, 2014
    Posts:
    22
    Tried that but with the same result!
     
  5. nolim

    nolim

    Joined:
    Mar 28, 2014
    Posts:
    22
    I think you might have hit the nail on the head! Time for a substantial re-write...
     
  6. BobBobson108

    BobBobson108

    Joined:
    Mar 13, 2008
    Posts:
    57
    Yes, what he said!
     
    nolim likes this.
  7. nolim

    nolim

    Joined:
    Mar 28, 2014
    Posts:
    22
    Thanks for the tips everyone, very much appreciated!
     
  8. ahmadian

    ahmadian

    Joined:
    Nov 24, 2015
    Posts:
    44
    Did you fixed it?
    I'm having the same problem
     
  9. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    You have to check for null before manipulating an object that is cast with "as".

    Example:
    Code (CSharp):
    1. GameObject ship = Instantiate(ShipToSpawn,location, rotation) as GameObject;
    2. if (ship != null)
    3. {
    4.     Enemies.Add(ship);
    5. }
     
  10. ahmadian

    ahmadian

    Joined:
    Nov 24, 2015
    Posts:
    44
    Thank you for your attention but that is not the case for me.
    the list is full but when I want to access it I cant find some Items even thou they are there
     
  11. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I'm confused, if you can't find the item then clearly it's not there.
     
  12. ahmadian

    ahmadian

    Joined:
    Nov 24, 2015
    Posts:
    44
    when I get the count of the list , the number is correct and when I put break points and debug the script even in the line where I'm trying to access it all of the elements are there but it still returns null
     
  13. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Please post some relevant code or it will be impossible for me to help you fix it.
     
  14. patrickcharlez

    patrickcharlez

    Joined:
    Sep 19, 2014
    Posts:
    1
    Not sure if this is helpful but i just had this problem. The problem was my class was Serializable and extended Monobehavior. It worked after i removed the " : MonoBehavior" from the class declaration.