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 thorskull · Jun 24, 2013 at 06:07 PM · iforder-of-execution

Scripting these if/else statements to operate correctly

First here's the code: using UnityEngine; using System.Collections; public class AnimationTest : MonoBehaviour { public GameObject leganim; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown(0)){ GetComponent<MegaModifyObject>().enabled = true; } else{ if (leganim.GetComponent<MegaPointCache>().time >= 2) { GetComponent<MegaModifyObject>().enabled = false; } } if ( Input.GetMouseButtonDown(0) && leganim.GetComponent<MegaPointCache>().time >= 2 ){ GetComponent<MegaModifyObject>().enabled = true; } if (leganim.GetComponent<MegaPointCache>().time >= 5) { GetComponent<MegaModifyObject>().enabled = false; } if ( Input.GetMouseButtonDown(0)){ GetComponent<MegaModifyObject>().enabled = true; } if (leganim.GetComponent<MegaPointCache>().time >= 8) { GetComponent<MegaModifyObject>().enabled = false; } //else{ //GetComponent<MegaModifyObject>().enabled = false; //} /*if (leganim.GetComponent<MegaPointCache>().time >= 10) { GetComponent<MegaModifyObject>().enabled = false; }*/ } }

Basically what is happening is I have three different scripts attached to a single object. With this AnimationTest script I am referencing the variable "time" in the MegaPointCache script and when that time is greater or equal to 2 seconds turn off the MegaModifyObject script. The MMO script starts out off; when I click anywhere it turns on and the MPC script proceeds to over 2 seconds (the developer has it increasing randomly to 6 decimal points; hence the greater than part) the MMO turns off. But when I try clicking to turn it back on and proceed to 5 seconds, it won't turn on and I can't even turn it on manually. I'm assuming this is due to the commands conflicting and since it is still over 2 seconds it won't let it turn on even though I'm telling it to. I don't know how to fix this, but I'm guessing it's pretty simple (hopefully).

Any ideas on how to fix this? Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MissingSemicolon · Jun 25, 2013 at 09:57 AM

         public GameObject leganim;
         private float intervalTime = 2.0f;
         private MegaPointCache theCache;
         private MegaModifyObject theObject;
 
         // public accessor if you want another class to maybe change the interval time?
         public float IntervalTime
         {
             get { return intervalTime; }
             set { intervalTime = value; }
         }
 
         void Start()
         {
             // store this here so we don't have to keep calling get component (its expensive!)
             theCache = leganim.GetComponent<MegaPointCache>();
 
             // same with the object
             theObject = GetComponent<MegaModifyObject>();
 
             // start the coroutine on start up
             StartCoroutine(SomeFunctionName());
         }
 
         void Update()
         {
 
             if (Input.GetMouseButtonDown(0))
             {
                 theObject.enabled = true;
             }
 
         }
 
         IEnumerator SomeFunctionName()
         {
             // this will loop like an update, you might want to replace true with a bool you have control of (to possibly stop this updating later)
             while (true)
             {
                 yield return new WaitForEndOfFrame();
 
                 if (theCache.time >= intervalTime)
                 {
                     theObject.enabled = false;
                 }
             }
         }

This is personally what i would do, i haven't had any chance to test this at all so apologies if it doesn't work or there are some syntax errors :)

I have split what you had in the Update into a coroutine, i prefer using these 90% of the time, makes it neater and gives you more control (among many other benefits). I recommend you look into coroutines they are very useful.

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

So to give a brief explanation to what i changed

  • GetComponent calls: You should always avoid calling GetComponent inside an Update loop, it's very expensive. Instead, assign these inside Start to an object that you can reuse.

  • intervalTime: I have added a variable called intervalTime, this is used to check against the MegaPointCache.time like you were before. Somewhere in your code you need to update this so it waits the correct amount of time (2, 5, 8, 10 etc) it avoids having numerous if statements for each seperate time.

Give it a try and let me know how it goes.

Comment
Add comment · Show 1 · 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
avatar image thorskull · Jun 25, 2013 at 12:39 PM 0
Share

Thanks! Overall it works fine without any bugs; I just have to figure how to update the time but that shouldn't be too hard. I had patched my original code by simply manually resetting the time so that it was outside the range of the if statement but this seems like it will work much better :)

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

16 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

Related Questions

How to make if/else statements progress in order 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unexpected token: if 1 Answer

if moving statement 1 Answer

Simple lerp roation problem 1 Answer


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