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 aldian · Mar 28, 2013 at 11:10 PM · c#getcomponentdisableenabled

Disabling scripts

Im trying to figure out how to disable one script to another, for example i have a game manager and when the timer hits zero i wish for the ai to be enabled, but also when script starts the ai is disabled. ive tried continously to implement diffrent methods but none seem to work for me :(

 using UnityEngine;
 using System.Collections;
 
 public class GAmemanager : MonoBehaviour
 {
 
     private float theTimer = 0.0f;
     private float theStartTimer = 20.0f;
     float displayMinutes;
     float displaySeconds;
      public GUISkin skin;
     public GameObject AI;
     
     
     
     void awake()
     {
         
 
         
         // failed version = AIBehaviour ai = (AIBehaviour)AI.GetComponent(typeof(AIBehaviour));
     }
     
     void Start ()
     {
         theTimer = theStartTimer;
     }
  
     void Update ()
     {
         theTimer -= Time.deltaTime;
  
         if (theTimer <= 0) {
             Debug.Log ("Over Time");
             
             theTimer = 0;
             
             
             //initiate battle phase
             
             //failed version = AIBehaviour.enabled = true;
             
             
         }
  
         if (Input.GetKeyUp (KeyCode.G)) 
         {
             Debug.Log ("Reset time");
             
             theTimer = theStartTimer;
             
       
         }
     }
  
     void OnGUI ()
     {
         GUI.color = Color.yellow;
         GUI.skin = skin;
         
         displayMinutes = Mathf.FloorToInt (theTimer / 60.0f);
         
         displaySeconds = Mathf.FloorToInt (theTimer % 60.0f);
         
         string text = string.Format ("{0:00}:{1:00}", displayMinutes.ToString ("0"), displaySeconds.ToString ("0"));
             
         GUI.Box(new Rect(10,100,100,25),"Time: " + text);
     }
 }
 
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 KuroiRoy · Mar 28, 2013 at 11:35 PM 0
Share

Have you tried this? AIBehaviour ai = AI.GetComponent();

or this? ai.enabled = true;

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by dorpeleg · Mar 28, 2013 at 11:39 PM

You should do it like this:

Define a var like:

 private AIBehaviour ai;

Then reference it like so:

 ai = AI.GetComponent<AIBehaviour>();

Then you can access it like:

 ai.active = false;

That should work.

Comment
Add comment · 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

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

12 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

Related Questions

Multiple Cars not working 1 Answer

C# GetComponent, component not turning off. 2 Answers

Distribute terrain in zones 3 Answers

Disable Scripts only works for one of the many 1 Answer

hi I know that this script is seen often but I have an another problem with a damage and health scripts. I think it's the GetComponent part, well anyway if you could help that would be great. 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