Change value on a GameObject using a script

Hi everyone,

So I’ve got a script used as a database for a gameobject.

It contains things like theses:

private bool canAttack = false;
public int hitPoints;
public int attackPower;

Using another script, I’d like to be able to change the value of canAttack to true. How could I do this?

If you script is called “XYZ” and it is attached to a gameObject, then if you have a reference to the gameObject, you can get the instance of XYZ attached to the gameObject using getComponent(XYZ)

see:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

for example:

var myXYZ:XYZ = GetComponent(XYZ);

you can then access the properties of your script through the myXYZ variable.

I’m not sure if I understand what you are suggesting.

Here’s what I’ve done up to now.

I’ve included a public variable like this:

public GameObject objOnScript;

And a private one:

private GameObject objReference;

And, in the Start(), I’ve tried to do this:

objReference = GetComponent(objOnScript);

But it doesn’t work and the console shows me this error:

Assets/Scripts/Game Scripts/SeekSteer.cs(24,32): error CS1502: The best overloaded method match for `UnityEngine.Component.GetComponent(System.Type)’ has some invalid arguments

What did I do wrong? And how could I correct it?

Finally got my answer, which was way simpler than I thought!

public EnemyController enemyController;

Made a variable of my other script. Then:

enemyController.canAttack = true;

And my console doesn’t sends me an error anymore. ^^