Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Elisenstein · Sep 20, 2020 at 04:38 PM · random.rangeintegerupdate problem

Set integer to new random value every time Update() runs

Hello! I am trying to have the value of an integer change randomly to either 0 or 1 every time a specific if statement within Update() is run, but the integer only seems to change the first time the if statement is run. I have already defined the integer at the beginning of the code, and I have also tried including hand_visible=1; within Start () to initialize it. Any help would be greatly appreciated!

Ex.

 Update ()
 
 if ((reset == true) && trial < 25)
      {
      hand_visible = Random.Range(0,2);
      }
 

Comment
Add comment · Show 6
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 Zaeran · Sep 20, 2020 at 04:43 PM 0
Share

Have you verified that the if statement is being called correctly?

avatar image Elisenstein Zaeran · Sep 20, 2020 at 04:55 PM 0
Share

Yes, the actual if statement is below and I can confirm that a new trial starts and the cube disappears. Even if I set hand_visible to an integer like 1 in this if statement an to a different integer in a separate if statement within Update(), the value of hand_visible does not seem to change

 if ((tip.transform.position).magnitude <= .5 * r && (reset == true) && trial < 25)
             {
 
                 hand_visible = 1;
 
                 reset = false;
                 theta = ((expOrder[trial, 0] + 1) * 20 + 30) * $$anonymous$$athf.PI / 180f;
                 Ball.Instance.gameObject.transform.localScale = new Vector3(0.025f, 0.025f, 0.025f);
 
                 Destroy(Cube.Instance.gameObject);
 
                 Ball.Instance.gameObject.transform.position = new Vector3(r * $$anonymous$$athf.Cos(theta), 0, r * $$anonymous$$athf.Sin(theta));
                 
avatar image Chrissyeah_ Elisenstein · Sep 20, 2020 at 04:58 PM 0
Share

All I can say currently is that I am quite sure hand_visible = Random.Range(0,2); works correctly, the only issue I can imagine is there is something else happening.

Show more comments
avatar image Chrissyeah_ · Sep 20, 2020 at 04:43 PM 0
Share

@Elisenstein

Can you confirm you are triggering the if statement? A Debug.Log("Triggered"); Should do

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Elisenstein · Sep 21, 2020 at 10:08 PM

Thank you for your help! There is a lot of commented out stuff that I have tried to do, but ultimately the hand_visible integer is not getting updated when the if statement is called. The ultimate goal is to have the hand prefab's mesh turn on/off randomly for each trial.

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using System.Net; using System.Net.Mail; using System.Security.Cryptography.X509Certificates; using System.Net.Security; using System.Collections.Specialized; using System.Security.Cryptography; using System; using Random = UnityEngine.Random; using System.ComponentModel;

 public class GameManager : MonoBehaviour
 {
     public bool rightHand = false; //false is left, true is right
 
     public GameObject ball;
     public GameObject cube;
     Collider m_Collider1;
     Collider m_Collider2;
 
     private bool reset = false; //reset is whether the hand is in the phase of the trial reaching for the ball, or before the next ball has spawned
    // private bool cubecollision = false;
     private int trial = 0;
     private float r = 0.4f;
     private float theta;
     private double error;
     private float[] initCoords;
     //private UnityEngine.Random rnd = new UnityEngine.Random();
     private Vector3 corVec = new Vector3(0.1f, 0.1f, 0.1f);
     private GameObject OVRCameraRig;
     private GameObject TrackingSpace;
     private GameObject LeftHandAnchor;
     private GameObject RightHandAnchor;
     private GameObject LeftFab;
     private GameObject RightFab;
     private GameObject Hand;
     private GameObject DisplayText;
     private GameObject Degree;
     private GameObject Error;
     private GameObject Trial;
     private GameObject Visibility;
     private GameObject BallPos;
     private GameObject HandPos;
     private GameObject Pinch;
 
     public static GameManager Instance;
 
     [SerializeField] private OVRSkeleton skeleton;
     private OVRHand hand;
     private Transform tip;
 
     private int[,] expOrder = new int[25, 2];
     private float[,,] data = new float[5, 5, 2];
     public string path;
  
     private int hand_visible;
 
 
 
 
 
     //Create text file that contains the erros for each trial
     void CreateText()
     {
         //Path of the file to save to Oculus Quest internal files
         path = "./sdcard/Android/data/com.TadinLab.ProVRb/Data" + System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".txt";
      
 
         //Create file if it doesn't exist
         if (!File.Exists(path))
         {
             File.WriteAllText(path, "List of Errors by Trial \n\n");
         }
     }
 
     
 
     // Start is called before the first frame update
     void Start()
     {
         //{
         //    //////
         //    //Check that the first GameObject exists in the Inspector and fetch the Collider
         //    if (cube != null)
         //        m_Collider1 = cube.GetComponent<Collider>();
 
         //    //Check that the second GameObject exists in the Inspector and fetch the Collider
         //    if (tip != null)
         //        m_Collider2 = tip.GetComponent<Collider>();
         //}
 
        
           
         
 
         //////
         {
             CreateText(); //calls the function to create the text file once the experiment starts
 
         }
 
         if (Instance == null)
         {
             Instance = this;
         }
 
         //init_coords[0] = CenterEye.Instance.gameObject.transform.position.x;
         //init_coords[1] = CenterEye.Instance.gameObject.transform.position.y;
         //init_coords[2] = CenterEye.Instance.gameObject.transform.position.z;
 
 
 
         for (int i = 0; i < 5; i++)
         {
             for (int j = 0; j < 5; j++)
             {
                 expOrder[i * 5 + j, 0] = i;
                 expOrder[i * 5 + j, 1] = j;
             }
         }
 
 
         Shuffle();
 
         OVRCameraRig = GameObject.Find("OVRCameraRig");
         TrackingSpace = OVRCameraRig.transform.GetChild(0).gameObject;
         LeftHandAnchor = TrackingSpace.transform.GetChild(4).gameObject;
         RightHandAnchor = TrackingSpace.transform.GetChild(5).gameObject;
         LeftFab = LeftHandAnchor.transform.GetChild(1).gameObject;
         RightFab = RightHandAnchor.transform.GetChild(1).gameObject;
 
 
 
         DisplayText = GameObject.Find("DisplayText");
         Visibility = DisplayText.transform.GetChild(0).gameObject;
         ///Degree = DisplayText.transform.GetChild(0).gameObject;
         ///Error = DisplayText.transform.GetChild(1).gameObject;
         Trial = DisplayText.transform.GetChild(2).gameObject;
         ///HandPos = DisplayText.transform.GetChild(3).gameObject;
         ///BallPos = DisplayText.transform.GetChild(4).gameObject;
         ///Pinch = DisplayText.transform.GetChild(5).gameObject;
 
         Visibility.GetComponent<TextMesh>().text = "vis: n/a";
         ///Degree.GetComponent<TextMesh>().text = "deg: n/a";
         ///Error.GetComponent<TextMesh>().text = "err: n/a";
         Trial.GetComponent<TextMesh>().text = "tri: 1";
 
 
         if (rightHand == false)
         {
 
             Hand = LeftHand.Instance.gameObject;
             skeleton = LeftFab.GetComponent<OVRSkeleton>();
             hand = LeftFab.GetComponent<OVRHand>();
             RightFab.gameObject.SetActive(false);
 
 
         }
 
         else
         {
 
             Hand = RightHand.Instance.gameObject;
             skeleton = RightFab.GetComponent<OVRSkeleton>();
             hand = RightFab.GetComponent<OVRHand>();
             LeftFab.gameObject.SetActive(false);
 
         }
 
        // hand_visible = 1;
         
 
 
 
 
 
 
 
         /////THIS ONE TURNS OFF HAND MESH
         //{
         //    RightFab.GetComponent<OVRMeshRenderer>().enabled = false; //makes hand invisible
         //}
 
        
 
 
 
         theta = UnityEngine.Random.Range(110f, 200f) * Mathf.PI / 180f;
         theta = ((expOrder[trial, 0] + 1) * 20 + 30) * Mathf.PI / 180f;
         Instantiate(ball, new Vector3(r * Mathf.Cos(theta), 0, r * Mathf.Sin(theta)), Quaternion.identity);
         ///Degree.GetComponent<TextMesh>().text = "deg: " + ((expOrder[trial, 0] + 1) * 20 + 30).ToString();
         //Instantiate(cube, new Vector3(0f, 0f, .2f), Quaternion.identity);
 
         //HandPos.GetComponent<TextMesh>().text = fingerBones.Count.ToString();
         //Instantiate(cube, new Vector3(0, 0, 0), Quaternion.identity);
 
 
         tip = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_IndexTip].Transform;
 
       //  hand_visible = 0;  
        
 
     }
 
      
     // Update is called once per frame
     void Update()
 
     {
 
 
 
         /// BallPos.GetComponent<TextMesh>().text = System.Math.Round(Ball.Instance.gameObject.transform.position.x, 2).ToString() + ","
         ///                                      + System.Math.Round(Ball.Instance.gameObject.transform.position.z, 2).ToString() + ",";
 
 
         /// HandPos.GetComponent<TextMesh>().text = System.Math.Round(tip.transform.position.x, 2).ToString() + ","
         ///                                     + System.Math.Round(tip.transform.position.z, 2).ToString() + ",";
 
         /// Pinch.GetComponent<TextMesh>().text = hand.GetFingerIsPinching(OVRHand.HandFinger.Index).ToString();
 
 
         //theta = Random.Range(40f, 160f) * Mathf.PI / 180f;
         //Ball.Instance.gameObject.transform.position = new Vector3(r * Mathf.Cos(theta), 0, r * Mathf.Sin(theta));
 
         
 
             if (hand_visible == 0)
             {
                 RightFab.GetComponent<OVRMeshRenderer>().enabled = false; //makes hand invisible
             }
 
             //else
             if (hand_visible == 1)
             {
                 RightFab.GetComponent<OVRMeshRenderer>().enabled = true; //makes hand visible
             }
 
             if ((tip.transform.position).magnitude >= r && (reset == false) && trial < 25)
             {
 
                hand_visible = 0;  
 
                  //error = Vector3.Distance(Ball.Instance.gameObject.transform.position, tip.transform.position);
                  error = Vector3.Angle(Ball.Instance.gameObject.transform.position, tip.transform.position);
                 
 
                  error = System.Math.Round(error, 3);
 
                 reset = true;
                 Ball.Instance.gameObject.transform.localScale = new Vector3(0f, 0f, 0f);
 
                 Instantiate(cube, new Vector3(0f, 0f, .2f), Quaternion.identity);
 
                 
 
                 trial++;
 
             //hand_visible = Random.Range(0, 2);
 
 
 
             Visibility.GetComponent<TextMesh>().text = "vis: " + hand_visible.ToString();
             ///Error.GetComponent<TextMesh>().text = "err: " + error.ToString();
             Trial.GetComponent<TextMesh>().text = "tri: " + (trial + 1).ToString();
                     //Cube.Instance.gameObject.transform.position = new Vector3(0, 0, .2f);
                 
 
                 {
 
                     //Content of the previously created text file
                     string content = "Error: " + error + "\r";
                     //Add some text to it by appending a new line after each trial
                     File.AppendAllText(path, content);
 
 
 
                 }
 
             }
 
 
   
 
 
         {
             if ((tip.transform.position).magnitude <= .5 * r && (reset == true) && trial < 25)
             {
 
                 hand_visible = 1;
 
                 reset = false;
                 theta = ((expOrder[trial, 0] + 1) * 20 + 30) * Mathf.PI / 180f;
                 Ball.Instance.gameObject.transform.localScale = new Vector3(0.025f, 0.025f, 0.025f);
 
                 Destroy(Cube.Instance.gameObject);
 
                 Ball.Instance.gameObject.transform.position = new Vector3(r * Mathf.Cos(theta), 0, r * Mathf.Sin(theta));
                 // Cube.Instance.gameObject.transform.position = new Vector3(0, 0, .5f);
 
                 ///Degree.GetComponent<TextMesh>().text = "deg: " + ((expOrder[trial, 0] + 1) * 20 + 30).ToString();
 
                 //hand_visible = Random.Range(0,2);
                 
                 //{
                 //    if (hand_visible == 0)
                 //    {
                 //        RightFab.GetComponent<OVRMeshRenderer>().enabled = false; //makes hand invisible
                 //    }
 
                 //    //else
                 //    if (hand_visible == 1)
                 //    {
                 //        RightFab.GetComponent<OVRMeshRenderer>().enabled = true; //makes hand visible
                 //    }
                 //    ///Error.GetComponent<TextMesh>().text = "err: " + error.ToString();
                 //    ///Trial.GetComponent<TextMesh>().text = "tri: " + (trial + 1).ToString();
                 //    //Cube.Instance.gameObject.transform.position = new Vector3(0, 0, .2f);
                 //}
 
             }
         }
 
         ////This will turn on hand mesh
         //RightFab.GetComponent<OVRMeshRenderer>().enabled = true; //makes hand visible
        
         
         if (trial >= 25)
         {
 
             Ball.Instance.gameObject.transform.localScale = new Vector3(0f, 0f, 0f);
 
         }
 
 
     }
 
         public void Shuffle()
         {
             int rand; ;
             int tempOne;
             int tempTwo;
 
             for (int i = 0; i < expOrder.GetLength(0); i++)
             {
                 rand = UnityEngine.Random.Range(0, expOrder.GetLength(0) - 1);
 
                 tempOne = expOrder[rand, 0];
                 tempTwo = expOrder[rand, 1];
 
                 expOrder[rand, 0] = expOrder[i, 0];
                 expOrder[rand, 1] = expOrder[i, 1];
 
                 expOrder[i, 0] = tempOne;
                 expOrder[i, 1] = tempTwo;
 
             }
         }
     }
 
Comment
Add comment · 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

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

136 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 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

How do I randomly change whether a mesh is on or off for an object, based on another object updating? 0 Answers

How do you add an integer to a random.range? 1 Answer

How can I make the Random.Range in this spawning script a PUBLIC adjustable variable? The (0, 35) code limit gives errors when I need the prefab list to fluctuate. An example code would help. Thanks! 1 Answer

reload random scene 2 Answers

Random String from list of string without repeated letters 2 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