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 $$anonymous$$ · May 29, 2014 at 06:35 PM · c#movement

adiing a wait betweem if statements

hello everyone. Sorry to be asking yet another question, but I have a piece of code, where you can move back and forth along the z axis, yet I want there to be a wait between switching from one position to another. here is my code: using UnityEngine; using System.Collections;

 public class dimensionswitcher : MonoBehaviour {
     float dimension1 = 0;
     float dimension2 = 1;
     void Update () {
             if (Input.GetKeyDown ("z"))
                 transform.position = new Vector3 (transform.position.x, transform.position.y, dimension1);
             if (Input.GetKeyDown ("x"))
                 transform.position = new Vector3 (transform.position.x, transform.position.y, dimension2);    
             }
                 }
Comment
Add comment · Show 1
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 Lo0NuhtiK · May 29, 2014 at 06:59 PM 0
Share

http://bit.ly/1oyWniC

"Note that you can't use yield from within Update or FixedUpdate, but you can use StartCoroutine to start a function that can." --Unity Script Reference

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by JayOhhh · May 29, 2014 at 07:20 PM

You need to use a Coroutine

 Do ();
 print ("This is printed immediately");
 
 function Do () {
     print("Do now");
     
     yield WaitForSeconds (2);
 
     print("Do 2 seconds later");
 }


You will need to adjust your logic a little bit to make it work though, but basically...

 public class dimensionswitcher : MonoBehaviour {
 
     float dimension1 = 0;
     float dimension2 = 1;
 
     void Update () {
 
     if (Input.GetKeyDown ("z")){
     // Call coroutine
     transform.position = new Vector3 (transform.position.x, transform.position.y, dimension1);
     } else
 
     if (Input.GetKeyDown ("x")){
     // Call Coroutine
     transform.position = new Vector3 (transform.position.x, transform.position.y, dimension2);
     }
 
 }


I have not fixed your logic for you to make it work, but you will need to move things around and use the coroutine to have a wait.

Also if you want a little bit of a hint you can take a look at my question I asked about coroutines:

http://answers.unity3d.com/questions/690943/shooting-bullets-onmousedrag-time-delay-issues.html

Hint is, it involves using a boolean.

Comment
Add comment · Show 4 · 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 $$anonymous$$ · May 29, 2014 at 08:32 PM 0
Share

thanks, that's just the thread I needed!

avatar image JayOhhh · May 30, 2014 at 02:59 PM 0
Share

Hah, I think you commented on the wrong thread :) But I can tell you your problem. If statements need to use comparison operators and not assignment operators (== vs =). (You are also missing the slashes on your comment.)

 //call waitswitch
 StartCoroutine (waitswitch());
 if (dimension1) {
 if (dimension2) {

Because they are booleans you don't need to have any comparison values in the if statement. If's are always asking for true false results. Because you have a boolean you can just go IF (boolean) or IF (!boolean).

Alternatively I believe you can go:

 call waitswitch
 StartCoroutine (waitswitch());
 if (dimension1 == true) {
 if (dimension2 == true) {

Also you are missing semi-colons everywhere! (line 6, 8, 23, 24, 26 and 2).

The other problem I see here is you still need to adjust your logic to work correctly. When you call your coroutine you are always setting both dimension variables to true. I don't think this is what you want.

avatar image $$anonymous$$ · May 31, 2014 at 02:14 AM 0
Share

Ok, I'm almost there now. Thanks to you all for bearing with me! I'm closer now, here's my new code: enter code hereusing UnityEngine; using System.Collections;

 public class dimensionswitcher : $$anonymous$$onoBehaviour {
     bool dimension1 = 0;
     bool dimension2 = 1;
     void Update () {
         call waitswitch;
         StartCoroutine (waitswitch());
         if (dimension1){    
         if (dimension2) {
         if (Input.Get$$anonymous$$eyDown ("z"))
                 transform.position = new Vector3 (transform.position.x, transform.position.y, dimension1);
             if (Input.Get$$anonymous$$eyDown ("x"))
                 transform.position = new Vector3 (transform.position.x, transform.position.y, dimension2);    
     }
         }
             }
                 }
 IEnumerator waitswitch(){
     (dimension1 == false);
     (dimension2 == false);
     yield return new WaitForSeconds (3.0f);
     (dimension1 == true);
     (dimension2 == true);
 }

but I get this error: Assets/dimensionswitcher.cs(21,30): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

What exactly is the problem here? It compiles in $$anonymous$$ono develop, yet unity won't run it.

avatar image JayOhhh · Jun 02, 2014 at 01:50 AM 0
Share

I suspect it's a problem with your assignment in the numerator.

== is for comparison

= is for assignment

(dimension == false) is the problem. You want to assign false to dimension. What you have is asking if dimension == false without an if statement.

I suggest reading up on C# and program$$anonymous$$g in general!

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

21 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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Problem with player movements 1 Answer

Jump and move (CharacterController.velocity) C# 0 Answers

Unity and Custom Defines 1 Answer

Character Rotation 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