Search Unity

[True] Each game object of the same script containing 1 static array has the same array

Discussion in 'Scripting' started by asperatology, May 27, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I put this script in different game objects scattered throughout the scene.

    Code (CSharp):
    1.  
    2. public class ArrayClass : MonoBehaviour {
    3.     public static int[] DummyArray = new int[10];
    4. }
    5.  
    I cannot tell if DummyArray is the same for all of the game objects containing the ArrayClass script component. Is it true or false?

    I mean, if I start the game, and I called on this function just once for the entire game:

    Code (CSharp):
    1.  
    2.     public void CalledOnceThroughoutEntireGame() {
    3.         DummyArray[0] = 10;
    4.         DummyArray[1] = 7;
    5.     }
    6.  
    Does that mean all other game objects containing the ArrayClass script component will have the same array as DummyArray, and its first 2 elements will be 10 and 7 respectively?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Static means there is only one instance.

    --Eric
     
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I know there's only one instance.

    But does it mean it's one unique instance for each individually different game objects?

    Prefab A has DummyArray. Prefab B has DummyArray. Both prefabs have the same MonoBehaviour script. Does that mean Prefab A has a unique static instance of DummyArray, and Prefab B has a unique static instance of DummyArray? Or the static instance of DummyArray is just one globally static instance of the game, and Prefab A and Prefab B both refer to the same global static DummyArray?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It means there's only one instance. If there was a unique instance for each object, then it would be more than one instance.

    --Eric
     
    NomadKing and Kiwasi like this.
  5. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Ok, sounds like it's one unique global instance for the duration of the entire game. Thanks.
     
  6. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Yeah, one global instance and you can acces it from other class via ClassName.Array