Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by blutackboy · Jan 21, 2016 at 10:10 PM · scripting problemnoobenable and disable script

enabling and disabling components script not working

Hi, Im new to c# and coding in general and I've made myself a script to effectively switch between two different types of movement (walking and flying), but it wasn't working as I could only switch to flying and not back so I edited it and now none work... I'm sure I'm missing something obvious or because I'm not very good at scripting at all :(

 using UnityEngine;
 using System.Collections;
 
 public class EnableScripts : MonoBehaviour { 
     private bool flymode;
     private bool walkmode
 
     void Update () {
     if (Input.GetKeyDown ("f"))
             GetComponent<Move> ().enabled = true;
         GetComponent<SpeedUp> ().enabled = true;
         GetComponent<WalkScript> ().enabled = false;
         flymode = true;
         {
     if (Input.GetKeyDown ("f") && flymode == true)
             GetComponent<Move> ().enabled = false;
         GetComponent<SpeedUp> ().enabled = false;
             GetComponent<WalkScript> ().enabled = true
         flymode = false;
      
   }
  }
 }   

 
Comment
Add comment · Show 2
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 SterlingSoftworks · Jan 21, 2016 at 10:39 PM 0
Share

If that's not working (don't know why it isn't), you could set your bools up there as "static" and inside of those functions you want messed with set up an if statement that checks that bool before doing the stuff inside of your move/fly functions.

avatar image blutackboy SterlingSoftworks · Jan 23, 2016 at 06:07 AM 0
Share

Thanks, I'll keep this in $$anonymous$$d for future reference.

1 Reply

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

Answer by corn · Jan 21, 2016 at 10:57 PM

Your code is quite messy, there are typos, and you didn't use brackets to delimit the if blocks, so there isn't much to change. However, always use GetComponent in Start or Awake, never in Update. You only need to get references to these scripts once, not every frame.

 using UnityEngine;
 using System.Collections;
  
 public class EnableScripts : MonoBehaviour 
 { 
     // Start in walk mode
     bool flymode = false;
     Move move;
     SpeedUp speedUp;
     WalkScript walk;
 
 
     void Awake()
     {
         // Get all references to your other components here
         move = GetComponent<Move>();
         speedUp = GetComponent<SpeedUp>();
         walk = GetComponent<WalkScript>();
     }
 
     void Update () 
     {
         // If player hits f...
         if (Input.GetKeyDown("f"))
         {
             // ... while they're walking, then start flying
             if (!flymode)
             {
                 move.enabled = true;
                 speedUp.enabled = true;
                 walk.enabled = false;
                 flymode = true;
             }
             // Else start walking again
             else
             {
                 move.enabled = false;
                 speedUp.enabled = false;
                 walk.enabled = true;
                 flymode = false;
             }
         }
     }
 }
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 blutackboy · Jan 22, 2016 at 05:43 AM 0
Share

Wow! Thank you so much! I really need to learn how to make my codes "cleaner" so thank you again and I'll be looking into this when I get time :D

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Dodgeroll script crashing game 0 Answers

Disable a script when its not hit by raycast. still uses 17 to 20% in profiler 0 Answers

Need help making a stamina bar. 0 Answers

Underwater Effect Help 1 Answer

Errors With Game Over Script 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