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 WirelessEStudios · Oct 14, 2016 at 08:05 PM · updatecomponentif statementenabledpublic variable

Disable a C# script from another C# script.

Hello! have a "Press S To Start" Script (Special thanks to @Mavina) But I also need to disable a script from that script. I have played around with the enabled property and but can't seem to get it to work. Here is what I have so far...

 using UnityEngine;
 using System.Collections;
 
 public class WaitForPlayer : MonoBehaviour
 {
     public GameObject waitScreen;
     public GameObject mainPlayer;
     public Component mainScript;
 
     // flag to determine if we are waiting for uset input to start game
     private bool waitingToStartGame = true;
 
     // Use this for initialization
     void Start ()
     {
         if (waitScreen != null)
         {
             waitScreen.SetActive(true);
         }
         else
         {
             waitingToStartGame = false;
             Debug.LogError("waitScreen was not set in the inspector. Please set and try again");
         }
         if (mainPlayer != null)
         {
             mainPlayer.SetActive(false);
             mainScript.enabled (false);
         }
         else
         {
             Debug.LogError("mainPlayer was not set in the inspector. Please set and try again");
         }
     }
 
     void Update ()
     {
         // if the waitingToStartGame is enabled and the 'S' key has been pressed
         if (waitingToStartGame && (Input.GetKeyDown(KeyCode.S)))
         {
             // set the flag to false so that will no longer be checking for input to start game
             waitingToStartGame = false;
             if (waitScreen != null)
             {
                 waitScreen.SetActive(false);
             }
             if (mainPlayer != null)
             {
                 mainPlayer.SetActive(true);
                 mainScript.enabled (true);
             }
         }
     }
 }

Thanks! :D

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
Best Answer

Answer by TBruce · Oct 14, 2016 at 08:22 PM

Try this

 using UnityEngine;
 using System.Collections;
 
 public class WaitForPlayer : MonoBehaviour
 {
     public GameObject waitScreen;
     public GameObject mainPlayer;

     // at a minimum the type needs to be Behaviour to get access to the variable 'enabled' but best practice is to use the actual type - e.g. MainScript
     public Behaviour mainScript;
 
     // flag to determine if we are waiting for uset input to start game
     private bool waitingToStartGame = true;
 
     // Use this for initialization
     void Start ()
     {
         if (waitScreen != null)
         {
             waitScreen.SetActive(true);
         }
         else
         {
             waitingToStartGame = false;
             Debug.LogError("waitScreen was not set in the inspector. Please set and try again");
         }
         if (mainPlayer != null)
         {
             mainPlayer.SetActive(false);
             // note: mainScript can not be on the same game object as mainPlayer, if it is then place the following above mainPlayer
             if (mainScript != null)
             {
                 mainScript.enabled = false;
             }
         }
         else
         {
             Debug.LogError("mainPlayer was not set in the inspector. Please set and try again");
         }
     }
 
     void Update ()
     {
         // if the waitingToStartGame is enabled and the 'S' key has been pressed
         if (waitingToStartGame && (Input.GetKeyDown(KeyCode.S)))
         {
             // set the flag to false so that will no longer be checking for input to start game
             waitingToStartGame = false;
             if (waitScreen != null)
             {
                 waitScreen.SetActive(false);
             }
             if (mainPlayer != null)
             {
                 mainPlayer.SetActive(true);
                 if (mainScript != null)
                 {
                     mainScript.enabled = true;
                 }
             }
         }
     }
 }

You should change this

 public Component mainScript;

to something like this

 public MainScript mainScript;

where MainScript is the actual class name.

Comment
Add comment · Show 8 · 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 WirelessEStudios · Oct 16, 2016 at 04:52 PM 0
Share

Hi! Thank you again for your valuable help! I will not get to it today but hopefully in the next couple days and when I do I will comment with feedback. Cheers :D

avatar image WirelessEStudios · Oct 19, 2016 at 08:57 PM 0
Share

Ok, so I gave it a go and I had to rearrange the if (mainScript != null) part because the script was in the mainPlayer. I am getting the error CS0246: The type or namespace name `$$anonymous$$ainScript' could not be found. Are you missing a using directive or an assembly reference?. Thanks again for your help :D

avatar image TBruce WirelessEStudios · Oct 19, 2016 at 09:13 PM 1
Share

Your original code had

 public Component mainScript;

As I stated above you need to change Component to the actual class name for mainScript. I do not know what it is but since you called the variable mainScript I just set the class name to $$anonymous$$ainScript. But you need to change the code to match whatever the class is named.

avatar image TBruce WirelessEStudios · Oct 22, 2016 at 05:36 PM 1
Share

The main reason that you can not use

 public Component mainScript;

is that Component does not have a variable called enabled. At a $$anonymous$$imum the type needs to be Behaviour to get access to the variable 'enabled' like so

 public Behaviour mainScript;

but best practice is to use the actual type - e.g. $$anonymous$$ainScript (I could only assume what the actual class name is going by the variable name provided). Using Behaviour will only limit your use of what you can do with mainScript.

avatar image WirelessEStudios TBruce · Oct 24, 2016 at 07:41 PM 0
Share

Thanks for your help and sorry for my delayed response. The script I am trying to disable and enable is a C# script (mainScript) in the $$anonymous$$ain Player. So while the game is waiting on the "press S to start" screen the timer in mainScript is stopped. I am not sure what class name to use for that to work. Thanks Again!

Show more comments

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

75 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

Related Questions

On Renderer Was Enabled? (C#) 0 Answers

Only Disable One Update on a Script 1 Answer

Why isnt the enabled property part of an interface? 0 Answers

Does the amount of if else statements within the Update matter? 0 Answers

NullPointException Object not found, caused by a variable not accepting value? 0 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