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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problems with random number

Discussion in 'Scripting' started by LarsArni, Apr 4, 2020.

  1. LarsArni

    LarsArni

    Joined:
    Apr 3, 2020
    Posts:
    8
    I'm trying to set a random number to decide where my character will move to, but it won't let me declare the random number outside of the Update() function. If I do it inside the Update function it will of course renew the random number every frame wich I don't want. I tried with a if statement, but then it will tell me it doesn't know the variable number, even though its in the Update() method, just in a if statement. The code down below is what I tried last, which is skipping the line of code. In the current itteration of the code, it knows the integer number in all the if statements except for the number == 1
    Thanks for your help!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using static Variables;
    5.  
    6. public class iTweenMovementTest : MonoBehaviour
    7. {
    8.     private Transform goal;
    9.     public bool setRandomNumber = false;
    10.     public int UpdateLoops = 0;
    11.     public bool allowed = true;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        /* GameObject officepoint = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    16.         goal = officepoint.transform;
    17.         officepoint.transform.position = new Vector3(46f, -0.1f, 21f);
    18.         officepoint.transform.localScale = new Vector3(0.1f, 0f, 0f);*/
    19.        
    20.     }
    21.     private void Awake()
    22.     {
    23.         transform.position = new Vector3(56f, 0.1f, 24f);        
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         if (UpdateLoops > 1)
    30.         {
    31.             goto myLabel;
    32.         }
    33.         int number = Random.Range(0, 5);
    34.      
    35.        
    36.  
    37.     myLabel:
    38.         if (number > 0)
    39.         {
    40.             if (number == 1)
    41.             {
    42.                 //office
    43.                 iTween.MoveUpdate(gameObject, new Vector3(46f, -0.1f, 21f), 10);
    44.                 Debug.Log("1");
    45.             }
    46.             else
    47.             {
    48.                 if (number == 2)
    49.                 {
    50.                     //warehouse
    51.                     iTween.MoveUpdate(gameObject, new Vector3(70f, -0.1f, 14f), 10);
    52.                     Debug.Log("2");
    53.                 }
    54.                 else
    55.                 {
    56.                     if (number == 3)
    57.                     {
    58.                         //supermarket
    59.                         iTween.MoveUpdate(gameObject, new Vector3(85f, -0.1f, 37f), 10);
    60.                         Debug.Log("3");
    61.                     }
    62.                     else
    63.                     {
    64.                         if (number == 4)
    65.                         {
    66.                             //cinema
    67.                             iTween.MoveUpdate(gameObject, new Vector3(37f, -0.1f, 37f), 10);
    68.                             Debug.Log("4");
    69.                         }
    70.                         else
    71.                         {
    72.                             Debug.Log("No random number between 1 and 4 was selected, check code for errors");
    73.                         }
    74.                     }
    75.                 }
    76.             }
    77.      
    78.         }
    79.         UpdateLoops++;
    80.     }
    81. }
    82.  
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,
    Are you trying to randomize movement at certain intervals of time or what?
     
  3. LarsArni

    LarsArni

    Joined:
    Apr 3, 2020
    Posts:
    8
    I have 4 positions, and I want to randomly select one of them and walk to that position. If it helps, I'm using iTween for the movement
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    On high level, one clean way to do it is to randomize a number, which you use to pick the direction. Then, move your object and only when the movement is done, randomize a new direction again.
     
  5. LarsArni

    LarsArni

    Joined:
    Apr 3, 2020
    Posts:
    8
    That's what I'm trying to do, but if I don't make the random number in the same method/loop, it doesn't work. It's really strange, I described it in my post
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Here's a simplified example. Hopefully this helps you to get the idea.
    If you use iTween or something else, you need to figure out how to implement it. You could also use Coroutines to move the object. iTween probably has some callbacks you could listen and then do something. Just the same as I measure the distance and stop moving when distance is too close.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class MoveToTargets : MonoBehaviour
    4. {
    5.  
    6.     public float speed = 2;
    7.     public float threshold = 0.01f;
    8.     bool moving;
    9.     Vector3 target;
    10.  
    11.  
    12.     void Update()
    13.     {
    14.         if (moving)
    15.             Move();
    16.         else
    17.             GetNewDirection();
    18.     }
    19.  
    20.  
    21.     void GetNewDirection()
    22.     {
    23.         int selectDirection = Random.Range(0, 4);
    24.  
    25.         switch(selectDirection)
    26.         {
    27.             case 0:
    28.                 StartMove(new Vector3(-10,0,0)); // Left
    29.             break;
    30.  
    31.             case 1:
    32.                 StartMove(new Vector3(10,0,0)); // Right
    33.             break;
    34.  
    35.             case 2:
    36.                 StartMove(new Vector3(0,0,10)); // Forward
    37.             break;
    38.  
    39.             case 3:
    40.                 StartMove(new Vector3(0,0,0)); // Home
    41.             break;
    42.         }
    43.     }
    44.  
    45.  
    46.     void StartMove(Vector3 t)
    47.     {
    48.         moving = true;
    49.         target = t;
    50.     }
    51.  
    52.  
    53.     void Move()
    54.     {
    55.         float step = speed * Time.deltaTime;
    56.         transform.position = Vector3.MoveTowards(transform.position, target, step);
    57.  
    58.         if (Vector3.Distance(transform.position, target) < threshold)
    59.         {
    60.             moving = false;
    61.         }
    62.     }
    63.  
    64. }
     
    LarsArni likes this.
  7. LarsArni

    LarsArni

    Joined:
    Apr 3, 2020
    Posts:
    8
    Thanks, but I would still like to new what I did wrong for the one number variable is considered wrong, the others not
     
  8. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Your code looks a bit convoluted. Start by cleaning it up -
    You don't need (and shouldn't in your case) make nested levels of If statements.
    Now you have:
    Code (CSharp):
    1.  if (number > 0)
    2.         {
    3.             if (number == 1)
    4.             {
    5.  
    6.             }
    7.             else
    8.             {
    9.                 if (number == 2)
    10.                 {
    11.  
    12.                 }
    Instead, make it:
    Code (CSharp):
    1. if (number == 1) {
    2.    // Do something with 1
    3. }
    4. else if (number == 2) {
    5.    // Do something with 2
    6. }
    7. else if (number == 3) {
    8.    // Do something with 3
    9. }
    10. else {
    11.   // You could have this too.
    12. }
    You don't necessarily have to consider number 0 at all, if you only test against 1,2,3,4 (for example.) I.e. if your ifs just handle cases for 1,2,3,4, they will not react to that 0.
    In this kind of setups you can use that else for just a cover all solution, which would handle everything else, like print error or throw an exception... or do something in the app.
     
    Last edited: Apr 5, 2020
    LarsArni likes this.