- Home /
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
Have you tried this? AIBehaviour ai = AI.GetComponent();
or this? ai.enabled = true;
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# GetComponent, component not turning off. 2 Answers
Distribute terrain in zones 3 Answers