How cache a script as a variable?

I’m working on a new zombie script but almost as soon as i started i’ve ran into a problem, i need to cache the “GameController” script as a varible for performance resons but i can’t get it to work, I’ve been looking up how to do it on this page but it just won’t work :frowning: Does anyone know how i could make this work?:

/*Bady script 2.0*/

#pragma strict
private var thisTransform : Transform;
private var player : GameObject;
private var fwd : Vector3;
private var controller : CharacterController;
private var GameController : MyScript;//I think I must define it here, cause it checks or something?

function Start () 
{
player = GameObject.FindWithTag ("Player");
thisTransform = transform;
fwd = thisTransform.TransformDirection(Vector3.forward);
controller = GetComponent(CharacterController);

GameController = player.GetComponent(GameController);//Here I need to get the "GameController" script

InvokeRepeating("main", 0, 0.1);
}

function main ()
{
    if (GameController.Chase == true)//But it cramps my style here.
     {
     }
}

Thank you :smile:

“It just won’t work” means…

My best guess is you’re naming your variable the same name as your class

var GameController
GameController.js

So when you’re calling it, it doesn’t know which you mean!

Are you getting an error or anything? Be sure your player variable isn’t null. And like Daniel said, it’s best to avoid naming your variable the samen as it’s type

I thought that at first to, but then i tried different names and it still doesn’t work :frowning:

The error is "The name ‘MyScript’ does not denote a valid type (‘not found’). " but then removing that I get the error “‘Chase’ is not a member of ‘Object’.”

_<

var variableName : VariableType;

So in your case:

var gameController : GameController;

Ohhhh, Thanks :smile: I did not realise that my script could be a variable type.

Yeahp, .js files are assumed to be classes unless you un-assume them. Is that a word? Sure, why not.

file: Thing.js

var x;

You can attach Thing to game objects (“file classes” are MonoBehaviours).

file: Thing2.js

class Thing2 {
  var x;
}

You can’t attach that to game objects (the class does not extend MonoBehaviour). But you can still use it as a type - this is helpful when you need some functionality that doesn’t make sense to be on a particular game object.