Problem with static array

Hello guys… I have a little problem over here with a public and static array…

The problem explained

I have a so-called “champion select”, where the players choose 3 characters to play with. After clicking one of the 10 buttons with all the characters, I put the champion ID in an array. This way, in the end, I will have an array called playerID with 7 elements (from 1 to 6, I don’t use position 0) and each element will tell me what champion the player wanted to play with… I put a DontDestroyOnLoad function for this script where I only declared the playerID and change the scene to the actual game. Afterwards, in the game, I have a sceneHandler empty object, where I take the information from the characterHandlerScript, and based on the array, I spawn those certain champions. The problem is only the first champion is spawned (out of all 6), whatever his model (I mean whatever the champion which the player has picked). Here is some code…

Part of the getCharacterToBox script

if (amCompletat == false && buton1.GetComponentInChildren<Text> ().text=="?") {
            buton1.GetComponentInChildren<Text> ().text = buttonWhichIHaveJustClickedOn.GetComponentInChildren<Text>().text;
            amCompletat = true;
            characterHandlerScript.playerID [1] = int.Parse (buton1.GetComponentInChildren<Text> ().text);
            print ("a");

        }

Explanation for getCharacterToBox

amCompletat is a variable that helps me see which of the 3 boxes are still incomplete. The buttons with characters each have text on them equal to a number. For instance, for the fourth champion, its button will have the text “4”. Everything seems cool when selecting the champions. I put the print(“a”) over there to see if the “if” statement is ran, and it is… But the array thingy got weird.

The whole characterHandlerScript

public static int[] playerID = new int[7];

    void Awake () {
        DontDestroyOnLoad (transform.gameObject);
    }

Part of the sceneHandler script

 for (int i = 1; i <= 6; i++)

         {

                if (characterHandlerScript.playerID[i] == 1)

                {

                    if (i <= 3)

                        spawnedPlayerpi = Instantiate(characterModel[1], playerSpawnPosition[i], Quaternion.identity) as GameObject;

                    else

                    {

                        spawnedPlayerpi = Instantiate(characterModel[1], playerSpawnPosition[i], Quaternion.identity) as GameObject;

                        spawnedPlayerpi.transform.Rotate(Vector3.up, 180f);

                    }

Explanation for the sceneHandler script

In the very first “if” I’m checking if the first character is character number 1. If so, I spawn him from the characterModel array with the position from playerSpawn Position. It’s pretty simple…

If you wish for any further explanation for this code, just let me know by leaving a comment… I’d really like to have someone helping me out with this problem…

I see your problem right away, I think! :slight_smile:
Here you are setting the variable:
but you need a variable to increment , maybe like this:

int selectcnt = 1;
characterHandlerScript.playerID [selectcnt] = int.Parse (buton1.GetComponentInChildren<Text> ().text);
            print ("a");
selectcnt++;
// maybe: if(selectcnt == 6) done

You are using ‘1’ instead of ‘i’ in your checks.
Here is a section to correct to give you an idea of what I mean. :slight_smile:

for (int i = 1; i <= 6; i++)
         {
// this should "..playerId[i] == i "
                if (characterHandlerScript.playerID[i] == 1)
                {
                    if (i <= 3)
                        spawnedPlayerpi =
//this should also be 'i' , ie: characterModel[i]
Instantiate(characterModel[1], playerSpawnPosition[i], Quaternion.identity) as GameObject;

Thanks a lot for showing interest in my problem, but unfortunately, that isn’t the solution. I showed those parts of code and wrote “part of the X script”, meaning that’s not the whole code, that’s only a part of it just so you guys could see how I operate"…

The code for getCharacterToBox is this:

public GameObject buttonWhichIHaveJustClickedOn;
    public GameObject buton1;
    public GameObject buton2;
    public GameObject buton3;
    public bool amCompletat;

    void Start () {
        amCompletat = false;
    }


    public void OnCharacterSelect()
    {

        if (amCompletat == false && buton1.GetComponentInChildren<Text> ().text=="?") {
            buton1.GetComponentInChildren<Text> ().text = buttonWhichIHaveJustClickedOn.GetComponentInChildren<Text>().text;
            amCompletat = true;
            characterHandlerScript.playerID [1] = int.Parse (buton1.GetComponentInChildren<Text> ().text);
            print ("a");

        }

        if (amCompletat == false && buton2.GetComponentInChildren<Text> ().text=="?") {
            buton2.GetComponentInChildren<Text> ().text = buttonWhichIHaveJustClickedOn.GetComponentInChildren<Text> ().text;   
            amCompletat = true;
            characterHandlerScript.playerID [2] = int.Parse(buton2.GetComponentInChildren<Text> ().text );
            print ("b");
        }

        if (amCompletat == false && buton3.GetComponentInChildren<Text> ().text=="?") {
            buton3.GetComponentInChildren<Text> ().text = buttonWhichIHaveJustClickedOn.GetComponentInChildren<Text> ().text;
            amCompletat = true;
            characterHandlerScript.playerID [3] = int.Parse(buton3.GetComponentInChildren<Text> ().text);
        }
    }

And, the code for sceneHandler is this:

 Vector3[] playerSpawnPosition = new[] { new Vector3(0f, 0f, 0f), new Vector3(-4f, 1f, -5f), new Vector3(0f, 1f, -5f), new Vector3(4f, 1f, -5f), new Vector3(-4f, 1f, 5f), new Vector3(0f, 1f, 5f), new Vector3(4f, 1f, 5f) };

    public static GameObject[] spawnedP = new GameObject[7];

    public GameObject[] characterModel = new GameObject[100];

    public GameObject[] characterIcon = new GameObject[7];
    public GameObject[] usedSkill = new GameObject[7];
    public GameObject[,] characterSkills = new GameObject[7, 5];

    GameObject spawnedPlayerpi;
    GameObject spawnedPlayerp1;
    GameObject spawnedPlayerp2;
    GameObject spawnedPlayerp3;
    GameObject spawnedPlayerp21;
    GameObject spawnedPlayerp22;
    GameObject spawnedPlayerp23;

    void Start () {

        for (int i = 1; i <= 6; i++)
         {
                if (characterHandlerScript.playerID[i] == 1)
                {
                    if (i <= 3)
                        spawnedPlayerpi = Instantiate(characterModel[1], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                    else
                    {
                        spawnedPlayerpi = Instantiate(characterModel[1], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                        spawnedPlayerpi.transform.Rotate(Vector3.up, 180f);
                    }
                    spawnedP[i] = spawnedPlayerpi;
                    declarari.P[i].currentHP = 120;
                    declarari.P[i].maxHP = 120;
                    declarari.P[i].maxMana = 50;
                    declarari.P[i].currentMana = 50;
                    declarari.P[i].manaRegen = 10;
                    declarari.P[i].ID = 1;
                }
                if (characterHandlerScript.playerID[i] == 2)
                {
                    if (i <= 3)
                        spawnedPlayerpi = Instantiate(characterModel[2], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                    else
                    {
                        spawnedPlayerpi = Instantiate(characterModel[2], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                        spawnedPlayerpi.transform.Rotate(Vector3.up, 180f);
                    }
                    spawnedP[i] = spawnedPlayerpi;
                    declarari.P[i].currentHP = 100;
                    declarari.P[i].maxHP = 100;
                    declarari.P[i].maxMana = 90;
                    declarari.P[i].currentMana = 60;
                    declarari.P[i].manaRegen = 20;
                    declarari.P[i].ID = 2;
                }
                if (characterHandlerScript.playerID[i] == 3)
                {
                    if (i <= 3)
                        spawnedPlayerpi = Instantiate(characterModel[3], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                    else
                    {
                        spawnedPlayerpi = Instantiate(characterModel[3], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                        spawnedPlayerpi.transform.Rotate(Vector3.up, 180f);
                    }
                    spawnedP[i] = spawnedPlayerpi;
                    declarari.P[i].currentHP = 80;
                    declarari.P[i].maxHP = 80;
                    declarari.P[i].maxMana = 100;
                    declarari.P[i].currentMana = 35;
                    declarari.P[i].manaRegen = 20;
                    declarari.P[i].ID = 3;
                }
                if (characterHandlerScript.playerID[i] == 4)
                {
                    if (i <= 3)
                        spawnedPlayerpi = Instantiate(characterModel[4], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                    else
                    {
                        spawnedPlayerpi = Instantiate(characterModel[4], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                        spawnedPlayerpi.transform.Rotate(Vector3.up, 180f);
                    }
                    spawnedP[i] = spawnedPlayerpi;
                    declarari.P[i].currentHP = 120;
                    declarari.P[i].maxHP = 120;
                    declarari.P[i].maxMana = 50;
                    declarari.P[i].currentMana = 50;
                    declarari.P[i].manaRegen = 0;
                    declarari.P[i].ID = 4;
                }
                if (characterHandlerScript.playerID[i] == 5)
                {
                    if (i <= 3)
                        spawnedPlayerpi = Instantiate(characterModel[5], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                    else
                    {
                        spawnedPlayerpi = Instantiate(characterModel[5], playerSpawnPosition[i], Quaternion.identity) as GameObject;
                        spawnedPlayerpi.transform.Rotate(Vector3.up, 180f);
                    }
                    spawnedP[i] = spawnedPlayerpi;
                    declarari.P[i].currentHP = 80;
                    declarari.P[i].maxHP = 80;
                    declarari.P[i].maxMana = 100;
                    declarari.P[i].currentMana = 90;
                    declarari.P[i].manaRegen = 15;
                    declarari.P[i].ID = 5;
                }
            }
    }

    void Update()
    {

    }

I see, that makes sense. I see how I overlooked that that was partially noticeable in your code before, too.
If you print out the list, are all of the values there as you’ve set them? (ie: champions selected).
On a side note, if we get this resolved, I could help you to clean that code up a little bit :slight_smile:
Even though it’s a bit longer than required, your code does look good. I will continue to look at it… and send this response in the meantime.

Thank you. If you want to be a part of our project (we are a team of people aged 16 to 17), you should know that we are a non-profit organization…

So I tried to print the values as you said, and now I notice that the values are printed and all of them are right. I tried to print them in the sceneHandler script.
I also tried to see if the players are spawned, but in the wrong position looking at the hierarchy while in play mode, but this is not the case. This means that I’m doing something wrong with this character model gameObject array…

Wasn’t I supposed to drag&drop the prefabs in the inspector at the character model section?

That is correct, simply drag them in there (but make sure to fill the spot zero (first spot) with either a duplicate or empty object, as you are avoiding the zero position/index).
If there’s still a problem , please try this to narrow the problem:
In your sceneHandler…

 for (int i = 1; i <= 6; i++)
         {
                print(" i = " + characterHandlerScript.playerID[i]);

I put a model in the characterModel array (in the inspector) even if I don’t use it…
I printed the values as you said in sceneHandler script, and they are printed right. So the values in the array are not the problem…

I should also say another thing about the compilation. After I press play, select my champions and start the battle, only the first character of the first player is spawned (no matter what champion he picked) and I get a Object reference not set to an instance of an object calling a certain line of sceneHandler, and that’s:

For instance, if I choose the first character of the first player to be the third champion, it spawns him and then gives me the error for line 80, which is:

declarari.P[i].currentHP = 80;

that comes from this if:

if (characterHandlerScript.playerID[i] == 3)

I am extremely confused right now and I really want to make this work because we already have all the mechanics for this game ready, we just need to implement UI and to solve this bug.

You know what… I should have asked you about that variable a while back, because I wondered what is that?!?
Those are obviously the champion’s stats, but they must be a script or other class that you wrote, right?
There is no reference to it, anywhere. So everything is working well & will work soon…
Show me the code where you have these ‘declarari’ things? :slight_smile:

Ok, sorry for answering so late, but I was stuck with another assignment of mine. i’m back now

Here’s a part of the code in declarari.cs. If you want all the file, let me know

 public class player
    {
        public int ID;
        public int currentHP;
        public int maxHP;
        public int currentMana;
        public int maxMana;
        public int manaRegen;
        public bool isDead;
        public bool adHocEffects;
        public increasedManaCost[] increasedManaRegen = new increasedManaCost[10]; // xNumber = lungimea vectorului x
        public int increasedManaRegenNumber;
        public skillEffect[] isInvulnerable = new skillEffect[10];
        public int isInvulnerableNumber;
        public skillEffect[] damageImmunity = new skillEffect[10];
        public int damageImmunityNumber;
        public skillEffect[] cannotBeInvulnerable = new skillEffect[10];
        public int cannotBeInvulnerableNumber;
        public skillEffect[] cannotReduceDamage = new skillEffect[10];
        public int cannotReduceDamageNumber;
        public skillEffect[] isInvulnerableToFriendly = new skillEffect[10];
        public int isInvulnerableToFriendlyNumber;
        public skillEffect[] isStunned = new skillEffect[10];
        public int isStunnedNumber;
        public skillEffect[] stunImmunity = new skillEffect[10];
        public int stunImmunityNumber;
        public increasedManaCost[] increasedManaCosts = new increasedManaCost[10];
        public int increasedManaCostsNumber;
        public increasedCooldown[] increasedCooldowns = new increasedCooldown[10];
        public int increasedCooldownsNumber;
        public incommingDamage[] increasedDamage = new incommingDamage[10];
        public int increasedDamageNumber;
        public incommingDamage[] increasedPiercingDamage = new incommingDamage[10];
        public int increasedPiercingDamageNumber;
        public incommingDamage[] increasedAfflictionDamage = new incommingDamage[10];
        public int increasedAfflictionDamageNumber;
        public incommingDamage[] incomingDamage = new incommingDamage[10];
        public int incomingDamageNumber;
        public incommingDamage[] incomingPiercingDamage = new incommingDamage[10];
        public int incomingPiercingDamageNumber;
        public incommingDamage[] incomingAfflictionDamage = new incommingDamage[10];
        public int incomingAfflictionDamageNumber;
        public incommingHeal[] incomingHeal = new incommingHeal[10];
        public int incomingHealNumber;
        public DD[] destructableDefense = new DD[10];
        public int destructableDefenseNumber;
        public DR[] damageReduction = new DR[10];
        public int damageReductionNumber;
        public skillEffect[] counter = new skillEffect[10];
        public int counterNumber;
        public skillEffect[] reflect = new skillEffect[10];
        public int reflectNumber;
        public skillEffect[] skillEffects = new skillEffect[50];
        public int skillEffectsNumber;
        public stoppableByStunSkillEffect[] stoppableByStunSkillEffects = new stoppableByStunSkillEffect[50];
        public int stoppableByStunSkillEffectsNumber;
    }

    public static player[] P = new player[7];

Okay. So by default, your player [ ] P array is filled with 7 ‘null’ values.
Somewhere in Awake, try this:

for(int i = 0; i < 7; ++i) P[i] = new player();

Let me know what happens when you re-run everything that way.

So I put void Awake () { for(int i = 1; i <= 6; i++) P[i] = new player(); in declarari.cs, and the weird “object reference not set to an instance of an object” error is still here when I run the game.

Is declarari a monobehaviour on an object in the scene? (So that its Awake function has been called?)

I cannot believe my eyes. It finally worked! Thanks a lot!! If you want, add me on skype: eric.spataru32
You are welcome into our project whenever you want.

I didn’t have the declarari.cs assigned on any of the gameobjects in the scene. No matter how much experience I gain in time, this is ALWAYS getting me…

You’re most welcome. Best wishes with your project. I hope that you & your friends accomplish great things!