- Home /
Saving Scripts to variables and MonoBehaviour
I have this code:
var enemyCount : int; var turretCount : int; var script : MonoBehaviour; var win : boolean;
function Awake () { script = GetComponent("SpawnScript"); }
function Update () { if (!win) { enemyCount = script.enemyCount; turretCount = script.turretsAlive;
if (enemyCount == 0 && turretCount == 0)
win = true;
}
}
function OnGUI() { if (GUI.Button (new Rect(400,300,200,100),"Play Again?")) Application.LoadLevel(0); }
Yet, when I try to save it, it says enemyCount and turretsAlive are not a part of MonoBehaviour. I don't get what I'm doing wrong.
Answer by Michael La Voie · Apr 14, 2010 at 10:17 PM
You need to change
var script : MonoBehaviour;
to
var script : SpawnScript;
This answer covers the details. Basically, you told the code that the component you're retrieving is a MonoBehaviour when in reality its a script.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163810im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Array Problem - Error Code BCE0022 2 Answers
Changing variables in GUI? 0 Answers
Gun Script Help 2 Answers
How to use Enum? 1 Answer