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 pottyscotty · Feb 09, 2015 at 10:42 AM · scripterror

Else if not working | C#

Hi all,

Im having problems with my C# Script, I want it so when a Key is held down, a sound and Gameobject are enabled. When the key is let go they both stop - Getting error "Unexpected Symbol 'Else'" (Sorry for the script going messy at the end)

 using UnityEngine;
 using System.Collections;
 
 public class Hose : MonoBehaviour {
 
     public GameObject SprayParticles; 
 
     public AudioSource HoseSound;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update() {
         
         if( Input.GetKeyDown( KeyCode.Z) ) {
             SprayParticles.SetActive(true);
             HoseSound.Play();
         }
     {
             
         else if ( Input.GetKeyDown( KeyCode.Z) )  
             {
                 SprayParticles.SetActive(false);
                 HoseSound.Stop();
             }
         }
     }
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
0
Best Answer

Answer by Joyrider · Feb 09, 2015 at 10:43 AM

You have a backet line 22 that should not be there. And same goes for line 29.

Hence your error.

Comment
Add comment · Show 3 · 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 pottyscotty · Feb 09, 2015 at 10:48 AM 0
Share

Cheers, I get rid of 22 and keep 29 and I get no errors, if I get rid of 29 I get a parsing error.

Even without the errors the script isn't working though. Pressing Z only enables the Game object and Sound, but when I let go it doesn't disable it.

EDIT:- I got rid of the Bracket on line 22 then changed the Input.Get$$anonymous$$eyDown in the Else if function to Input.Get$$anonymous$$eyUp. Works fine.

avatar image KFriske Studios · Feb 09, 2015 at 05:30 PM 0
Share

It is because it will only turn it on, you have to set the "else if" to be different than the "if" otherwise it will only do the "if" so your new code would be if( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Z) && !SprayParticles.enabled) { SprayParticles.SetActive(true); HoseSound.Play(); } { else if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Z) )&& SprayParticles.enabled { SprayParticles.SetActive(false); HoseSound.Stop(); } }

avatar image pottyscotty · Feb 09, 2015 at 07:40 PM 0
Share

Cheers, I used this method to do it (Changing Get$$anonymous$$eyDown to Get$$anonymous$$eyUp on the Else if section (Changed to Button control now))

 using UnityEngine;
 using System.Collections;
 
 public class Hose : $$anonymous$$onoBehaviour {
 
     public ParticleEmitter SprayParticles; 
 
     public AudioSource HoseSound;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update() {
         
         if (Input.GetButtonDown ("Fire1")) {
             HoseSound.Play();
             SprayParticles.emit = true;
         }
 
             
         else if (Input.GetButtonUp ("Fire1"))  
             {
                 HoseSound.Stop();
                 SprayParticles.emit = false;
             }
         }
 }

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

20 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

Related Questions

merry fragmas part 2 PlayerNetworkMover Script errors 1 Answer

Add script to created gameobject via script 0 Answers

[RPC] identifier expected 1 Answer

GetComponent won't work 1 Answer

Script open help? 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