Search Unity

gameobject and renderer arrays

Discussion in 'Scripting' started by overthrowrobotics, Jul 28, 2015.

  1. overthrowrobotics

    overthrowrobotics

    Joined:
    Jun 19, 2015
    Posts:
    47
    I have 48 different gameobjects that I want to enable and disable based on data I read from a serial port.

    I created 48 gameobjects, 48 renderers, 48 gameobject.find but that's a bit messy.

    I tried the following but it doesn't seem to be working.

    Code (CSharp):
    1. GameObject[] chestSensors;
    2. private Renderer[] chestRend;
    3. chestSensors = GameObject.FindGameObjectsWithTag ("chestSensors");
    4.  
    5. for (int a = 1; a< 49; a++) {
    6. chestRend [a] = chestSensors[a].GetComponent<Renderer> ();
    7. }
    I get this error though. NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object)
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Arrays indices start at 0, not 1, so that's going to cause an error when you get up to checking #48. It fails before that right now though, because you haven't actually initialized the chestRend array yet. You need to say:
    Code (csharp):
    1. private Renderer[] chestRend = new Renderer[chestSensors.length];
    or just initialize it directly to 48 if you know for certain there are, and always will be, exactly 48 objects with the tag "chestSensors".

    You can also skip all of this if the Renderers are on child objects of the current object. You can then just call GetComponentsInChildren<Renderer>() to get an array of all of the renderers directly (this WILL include the Renderer on the current object as well, if one exists, so be wary of that).
     
  3. overthrowrobotics

    overthrowrobotics

    Joined:
    Jun 19, 2015
    Posts:
    47
    Awesome. Thank you. That worked. The problem I have now is that the gameobjects aren't sorted. I named them 1 through 48 and then used this:

    chestSensors = GameObject.FindGameObjectsWithTag("chestSensors").OrderBy( go => go.name ).ToArray();

    But it seems as if they are randomly assigned. Maybe it's going like this

    1,10,11,12,13,14,15,16,17,18,19,2,20,21... ?? Alphabetically and not numerically?
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Nope, it doesn't use the name for organizational purposes at all- it'll likely load them by their internal ID numbers, but it doesn't matter. Most of the time the "name" of a GameObject is completely meaningless beyond a means of making visual organization easier in the scene (for you, not the program). If you want them sorted (and since you're using GameObjects and not ints or some other very small value type), it would be better in the long-run if you made the array into a List and then just ran the Sort function to organize it by name.

    If you're adamant about leaving it as a array, you can run "System.Array.Sort(arrayName);" instead.

    EDIT: Skimmed over it and missed the OrderBy() completely, lol.
     
    Last edited: Jul 29, 2015
  5. overthrowrobotics

    overthrowrobotics

    Joined:
    Jun 19, 2015
    Posts:
    47
    When I put in System.Array.Sort(chestSensors);

    I get Object reference not set to an instance of an object on this line:

    Code (csharp):
    1.  
    2. for (int b=0; b< 48; b++)
    3.         {
    4. ***          chestRender[b].enabled = false;
    5.         }
    6.  
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    ".enabled" is deprecated, you need to use ".SetActive()" instead. This is still referring to the chestRender array, not the chestSensors array, so that change is irrelevant. Also, wasn't this called "chestRend" in a previous post? Don't give me snippets if you're having problems though, please post the whole thing.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Name is a string, so it gets sorted alphabetically. You could parse the name to an int (bad idea in the long run). Or you could add a variable to a script that is a sorting ID and use that.
     
  8. overthrowrobotics

    overthrowrobotics

    Joined:
    Jun 19, 2015
    Posts:
    47
    I was able to get it to work with the following:

    Code (csharp):
    1.  
    2. chestSensors = GameObject.FindGameObjectsWithTag("chestSensors").OrderBy( go => int.Parse(go.name) ).ToArray();
    3.  
    Thanks everyone for the help.