- Home /
.enabled Problems
This code:
using UnityEngine;
using System.Collections;
public class DisableMoveWhileFlying : MonoBehaviour {
void Start(){
obj = GetComponent<Move>();
}
void OnCollisionExit(){
obj.enabled = false;
}
void OnCollisionEnter(){
obj.enabled = true;
}
}
Throws this error (twice):
error CS1061: Type `UnityEngine.Component' does not contain a definition for `enabled' and no extension method `enabled' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
What am I doing wrong?
Comment
Best Answer
Answer by YoungDeveloper · Apr 17, 2015 at 12:17 AM
Where are you caching your reference? Any class - component which inherit from MonoBehaviour will contain .enabled.
public class DisableMoveWhileFlying : MonoBehaviour {
private Move obj;
void Start(){
obj = GetComponent<Move>();
}
void OnCollisionExit(){
obj.enabled = false;
}
void OnCollisionEnter(){
obj.enabled = true;
}
}
I'm sorry. That does work, but what do you mean by "caching your reference"?
You store component reference in $$anonymous$$ove obj; and that is what you forgot.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Disabled script still working C# 3 Answers