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. Dismiss Notice

Random.Range alway return 0

Discussion in 'Scripting' started by tienanhnguyen996, Jul 16, 2019.

  1. tienanhnguyen996

    tienanhnguyen996

    Joined:
    Jul 16, 2019
    Posts:
    1
    I want my object to respawn at an position with a random y but the random alway return 0
    here is my code
    thank you and sorry about my bad english
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Random = UnityEngine.Random;
    5.  
    6. public class Respawn : MonoBehaviour
    7. {
    8.     private float speed = 4.5f;
    9.     public float minY= -0.8f;
    10.     public float maxY= 0.3f;
    11.     void Start()
    12.     {
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         transform.Translate(speed * -Time.deltaTime, 0, 0);
    19.     }
    20.     void OnTriggerEnter2D(Collider2D other)
    21.     {
    22.         float randomNumber = 0;
    23.         randomNumber = Random.Range(minY, maxY);
    24.         Debug.Log("Random Number is = " + randomNumber);
    25.         if (other.transform.tag.Equals("Respawn"))
    26.         {
    27.             transform.position = new Vector3(5, randomNumber, 0);
    28.         }
    29.     }
    30. }
    31.  
     
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729
    Have you checked the values for Min and Max Y that have been serialised in the inspector? If you changed those values after you first created the script, they would have been serialised as 0.
     
    tienanhnguyen996 likes this.
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Hmmm. have you tried to remove the "using Random = UnityEngine.Random" clause?

    Also, note that your code will only Change the own object's Position to the new values if the other object has the "Respanwn" tag.
     
  4. DranrebKing

    DranrebKing

    Joined:
    Jun 3, 2018
    Posts:
    4
    Check the minY and maxY in the Inspector, maybe it's defaulted to 0.
     
    Munchy2007 likes this.
  5. ADZ_

    ADZ_

    Joined:
    Jun 14, 2019
    Posts:
    7
    This code might help. I found it from a Unity Answers post years ago and modified it slightly.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public static class MyRandom
    6. {
    7.     static System.Random rnd = new System.Random();
    8.  
    9.     public static int Range(int min, int max)
    10.     {
    11.         return min + rnd.Next() % (max + 1);
    12.     }
    13.  
    14.     public static float Range(float min, float max)
    15.     {
    16.         return min + rnd.Next() % (max + 1);
    17.     }
    18. }
     
  6. thorham3011

    thorham3011

    Joined:
    Sep 7, 2017
    Posts:
    25
    @ADZ_: Avoid using System.Random, it's broken. Unity's random number generator is much better, so it's better to fix the issue.