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 21, 2020 at 10:04 PM · random.rangemeshrendererintegerupdate problem

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

I have created an integer hand_visible that is supposed to alternate between = 0 or 1 when another object updates, but it only seems to work for the very first time the object updates.

 public class GameManager : MonoBehaviour
 {
     public bool rightHand = false; //false is left, true is right
 
     public GameObject ball;
     public GameObject cube;
 
     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 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;
   
 
     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 int hand_visible;
 
     // Start is called before the first frame update
     void Start()
     {
         if (Instance == null)
         {
             Instance = this;
         }
 
         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;
 
         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);
 
         }
 
         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);
       
         tip = skeleton.Bones[(int)OVRSkeleton.BoneId.Hand_IndexTip].Transform;
 
     }
 
      
     // Update is called once per frame
     void Update()
 
     {
 
 
 
       
         {
             if ((tip.transform.position).magnitude >= r && (reset == false) && trial < 25)
             {
                 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);
                
    
                 {
                     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 <= .5 * r && (reset == true) && trial < 25)
             {
 
 
                 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));
                 }  
         
         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
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

0 Replies

· Add your reply
  • Sort: 

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

134 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

Related Questions

Set integer to new random value every time Update() runs 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

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

Dealing with Texture2D.width as it is a float number 3 Answers

Why is there sometimes an f behinf a number? 3 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