So, I’m curious as too if there is a way to add variables to a game object.
For Example
GameObject mob;
mob.currentHealth
mob.currentMana
mob.maxHealth
mob.maxMana
mob.experience
So, I’m curious as too if there is a way to add variables to a game object.
For Example
GameObject mob;
mob.currentHealth
mob.currentMana
mob.maxHealth
mob.maxMana
mob.experience
This is what I was describing. Create 2 scripts, one called Mob and one called AccessMobClass (or whatever you like). In a blank scene, attach AccessMobClass to the camera or an empty gameObject (this is just a test).
Mob.js
#pragma strict
public class Mob extends MonoBehaviour
{
public var someInt : int = 0;
public var someFloat : float = 0.0;
}
AccessMobClass.js
#pragma strict
public class AccessMobClass extends MonoBehaviour
{
public var mob1 : Mob;
public var mob2 : Mob;
function Start()
{
mob1 = new Mob();
mob2 = new Mob();
mob1.someInt = 1;
mob1.someFloat = 2.5;
mob2.someInt = 5;
mob2.someFloat = 9.125;
Debug.Log( "mob1 : someInt = " + mob1.someInt + " : someFloat = " + mob1.someFloat );
Debug.Log( "mob2 : someInt = " + mob2.someInt + " : someFloat = " + mob2.someFloat );
}
function Update()
{
if ( Input.GetMouseButtonDown(0) )
{
mob1.someInt += 1;
mob1.someFloat += 2.5;
mob2.someInt += 5;
mob2.someFloat += 9.125;
Debug.Log( "Update mob1 : someInt = " + mob1.someInt + " : someFloat = " + mob1.someFloat );
Debug.Log( "Update mob2 : someInt = " + mob2.someInt + " : someFloat = " + mob2.someFloat );
}
}
}
Then run the scene, and check the console. Now each time you press the LMB, the values will be updated and displayed in the console.
note : this does give a warning in Unity :
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
which I find very strange as this works. Perhaps one of the great minds on this 'site can explain why and/or how to remove this warning =]