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[i]);
clone.GetComponent<P1Move>().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[i]);
clone.GetComponent<P1Move>().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?
I apologize if I break any posting rules
Ideally, individual help Q's should be in the Help Room area. But it's been years since anyone followed the written rules. If you read them through in the FAQ, you'll see the rules even contradict themselves.
did not know you could use less than or equal to in a for loop
Gah! Skim any book on C# from your library. It will include all the basics like that. Internet-wise, I like taxesforcatses-dot-com. But they are lots of C# sites (but many are not-so-good. Just cut-and-paste with errors in them.)
Answer by tanoshimi · Jul 17, 2017 at 07:36 PM
'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.
ok good to know. here is some more information. I will have a "menu" scene where you can select the number of players via buttons. a 2 players button, a 3 players button, and a 4 players button. once a button is clicked the game scene will load and the "numofplayers" variable should receive the proper integer (this part i have yet to do).
hence the need for the loop to deter$$anonymous$$e how many players to instantiate.
does this clarify?
also my spawn points are 4 separate empty game objects. is my numofplayers being changed by this is this line changing my numofplayers? instantiating at all spawnpoints regardless of the loop?
clone = Instantiate(player,spawnpoints[i]);
Check the value in the inspector for the object in question.
ah yes you are correct the value in the inspector was 4. so how do i go about changing that variable if its only done in the inspector. i will need the menu to tell the game controller that correct value for numofplayers
Answer by FortisVenaliter · Jul 17, 2017 at 07:28 PM
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.