Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by zhavens88 · Jul 17, 2017 at 07:22 PM · instantiatevariablefor-loop

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 17, 2017 at 07:58 PM 0
Share

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.)

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zhavens88 · Jul 17, 2017 at 09:27 PM 0
Share

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]);

avatar image FortisVenaliter zhavens88 · Jul 17, 2017 at 09:28 PM 0
Share

Check the value in the inspector for the object in question.

avatar image zhavens88 FortisVenaliter · Jul 17, 2017 at 09:43 PM 0
Share

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

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zhavens88 · Jul 17, 2017 at 07:43 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating more than one column(array)? 1 Answer

Instantiating mutiple prefabs using rotation of another gameObject + a gradually increaseing rotation offset 1 Answer

Assign GameObject OnMouseUp not through inspector -2D 1 Answer

Instantiate a script into an instantiated prefab 3 Answers

IEnumerator and for loop not working [noob] What do I do? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges