For loop and Instantiate issue

SOLVED: The variable “numofplayers” being public was the issue. public variables can only be changed in the editor. apparently the script will not change it. I removed public from the variable and my for loop was able to read it

This is my first post. I apologize if I break any posting rules. I am a beginner programmer but I understand the concepts well enough.

I’m making a local multiplayer game and I want to instantiate the proper number of players, with a for loop, based on a variable “numofplayers”. each player gets there respective "playerNum’ (max players will be 4)
i.e. “numofplayers = 2” then instantiate 2 players. the players will instantiate at “spawnpoints” which are empty game objects as usual.

my problem is the for loop is instantiating all 4 players when i use the variable “numofplayers” no matter what number i set it. if I manually enter the number into the for loop it instantiates the proper number.

my thought is THIS script should instantiate 2 players, but it instantiates all 4

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class gameController : MonoBehaviour {
    
    public GameObject player;
    public Transform[] spawnpoints;
    public int numofplayers = 2;
    

	// Use this for initialization
	void Start () {

        for (int i = 0; i < numofplayers; i++)
        {
            GameObject clone;
            clone = Instantiate(player,spawnpoints*);*

clone.GetComponent().playerNum = i;
}
}
When i exchange “numofplayers” in the for loop with an actual integer like THIS. it instantiates 2
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class gameController : MonoBehaviour {

public GameObject player;
public Transform[] spawnpoints;
public int numofplayers = 2;

  • // Use this for initialization*
  • void Start () {*

for (int i = 0; i < 2; i++)
{
GameObject clone;
clone = Instantiate(player,spawnpoints*);*
clone.GetComponent().playerNum = i;
}
}
To reiterate. Why does the for loop not use the “numofPlayers” variable properly. the for loop works properly with an integer but not when i substitute the variable.
Thank you in advance for the help
bonus question: spawnpoints[] is a transform array. arrays start at 0. why does 2 in the for loop instantiate 2 player and not 3? is it because the foor loop starts at 0?

‘public’ variables in Unity are initialized in the inspector, not in code. So even though you write this:

public int numofplayers = 24524;

It will have no effect if, in the inspector for that component, you’ve set the value to 2.

They should work identically. I think your value is getting changed somewhere. Try adding the following line before the for loop and note it’s output when you run the game:

Debug.Log(numofplayers);

As for the second question, yes, it’s because it starts at zero. You start at zero and test if it’s less than two. So, the valid indices raised will be 0 and 1, for a total count of two. If you did less than or equal, you would get three values.

thank you for the quick response. you are correct when when i run the debug.log the variable is being changed to 4. VERY ODD. there is absolutely nothing in my code that should be changing it. no other scripts use that variable. that is literally the only 2 places the variable is written.

i will be afk for an hour or so. when i return i can post more of my sript/scripts. if your willing maybe we can figure this out

awesome i did not know you could use less than or equal to in a for loop.