Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Random value is always same.

Discussion in 'Multiplayer' started by Slack43, Feb 21, 2018.

  1. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    Hi, so i have this code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClassManager : MonoBehaviour {
    6.     [SerializeField]private int classId = 0;
    7.     private int seekers = 0;
    8.     private int hiders = 0;
    9.     GameObject player;
    10.  
    11.     public Color hiderColor;
    12.     public Color seekerColor;
    13.  
    14.     [SerializeField] Material playerMaterial;
    15.     [SerializeField] private int maxClass = 2;
    16.  
    17.     void Start(){
    18.         classId = Random.Range (1, maxClass);
    19.         player = this.gameObject;
    20.     }
    21.     void Update(){
    22.         if (classId == 1) {
    23.             if (hiders < 2) {
    24.                 playerMaterial.color = hiderColor;
    25.                 hiders++;
    26.             } else {
    27.                 classId = 2;
    28.             }
    29.         }
    30.         if (classId == 2) {
    31.             if (seekers < 3) {
    32.                 playerMaterial.color = seekerColor;
    33.                 seekers++;
    34.             } else {
    35.                 classId = 1;
    36.             }
    37.         }
    38.         Debug.Log (classId.ToString ());
    39.     }
    40.        
    41. }
    42.  
    classId is always equal to 1. Sometimes it's equal to 2 but it directly changes to 1, i think it's something with networking or i just made huge error somewhere;
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're just using Random.Range wrong. For integers the max input is exclusive. So when you call Random.Range(1,2) the only possible return value is 1. If you want to include that max value do this:

    Code (csharp):
    1. classId = Random.Range (1, maxClass + 1);
    The above where maxClass == 2 will return a random value of either 1 or 2.

    https://docs.unity3d.com/ScriptReference/Random.Range.html
     
  3. Slack43

    Slack43

    Joined:
    Dec 23, 2015
    Posts:
    68
    Yes i just fixed that like yesterday ;D but thanks btw.