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

Question RandomRange in void update

Discussion in 'Getting Started' started by unity_B334D70C57C393E20990, Aug 29, 2023.

  1. unity_B334D70C57C393E20990

    unity_B334D70C57C393E20990

    Joined:
    Aug 27, 2023
    Posts:
    5
    Hello, ist it possible to put randomrange in void update() im trying to obtain a new random value everytime an event happens,, but its only letting me put Random.Range in void (start) , thank you! Code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Runtime.CompilerServices;
    4. using Unity.VisualScripting;
    5. using UnityEngine;
    6.  
    7. public class CharacterCtrl : MonoBehaviour
    8. {
    9.     public int Speed;
    10.     public int JumpForce;
    11.     public GameObject Character;
    12.     public GameObject Coin;
    13.     public Rigidbody2D rb2D;
    14.     public float Countdown = 3f;
    15.     public float Coinx = Random.Range(10f, 100f);
    16.     public float Coiny = Random.Range(10f, 100f);
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         rb2D.Sleep();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.        
    27.         void Timer ()
    28.         {
    29.             Countdown = Countdown - Time.deltaTime;
    30.         }
    31.  
    32.         Timer();
    33.  
    34.         if (Countdown <= 0)
    35.         {
    36.             rb2D.WakeUp();
    37.         }
    38.         if (Input.GetKeyDown(KeyCode.A) && Countdown <= 0 || Input.GetKeyDown(KeyCode.LeftArrow)&& Countdown <= 0)
    39.         {
    40.             rb2D.velocity = Vector2.left * Speed;
    41.         }
    42.  
    43.         if (Input.GetKeyDown(KeyCode.D) && Countdown <= 0 || Input.GetKeyDown(KeyCode.RightArrow) && Countdown <= 0)
    44.         {
    45.             rb2D.velocity = Vector2.right * Speed;
    46.         }
    47.  
    48.         if (Input.GetKeyDown(KeyCode.Space) && Countdown <= 0)
    49.         {
    50.             rb2D.velocity = Vector2.up * JumpForce;
    51.         }
    52.  
    53.     }
    54.  
    55.     void OnTriggerEnter2D(Collider2D collider)
    56.     {
    57.         if (collider.gameObject == Coin)
    58.         {
    59.             Instantiate(Coin, new Vector2(Coinx,Coiny), transform.rotation);
    60.         }
    61.     }
    62. }
    63.  
    64.  
    65.  
    Error message: UnityException: Range is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'CharacterCtrl'.

    Editor version: Unity 2022.3.8f1; Direct X version 11
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    This has nothing to do with Update()
    You can NOT call Random.Range when declaring a value. You need to declare it and then fill the value with Random.Range in Start((
     
    bugfinders likes this.
  3. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Keep the declaration at the top, without setting a value.

    Set a value, using Random.Range, in your ``` if (collider.gameObject == Coin)```
    loop. I assume you want to initiate every coin at a random spot.
     
  4. Saeed-B

    Saeed-B

    Joined:
    Jul 12, 2021
    Posts:
    46
    Worth noting that if you do actually need a random value when declaring the field, you can use `new System.Random()` instead.
     
    DevDunk likes this.