I’m trying to change a variable in a class from another game object, but I can’t seem to figure out how.
For example, let’s say I want to change the maxForwardSpeed variable of the CharacterMotorMovement Class from another script, on another game object. How would I access it and change the speed.
SomeScript ss = (SomeScript)GameObject.GetComponent(typeof(SomeScript));
ss should now be accessible as long as your class is public/internal or protected in an inherit, so if SomeScript have a field called Speed just do this:
function AddGernade() {
var playerScriptRequest = GameObject.Find("Player");
var playerScriptFunction = playerScriptRequest.GetComponent("PlayerScript");
playerScriptFunction.inventory
}
I’m trying to access the variable timeGernades within the Inventory Class in PlayerScript. I’m stuck on inventory, don’t know how to access a variable within Inventory.
This is what the class looks like in the PlayerScript.js
class PlayerInventory {
var timeGernades : int;
var fragGernades : int;
}
var inventory : PlayerInventory = PlayerInventory();
The first is that you have to tell Unityscript to make a variable accessible to other scripts. It doesn’t do it automatically. You need to change your “PlayerScript” as follows:
class PlayerInventory {
public var timeGernades : int;
public var fragGernades : int;
}
var inventory : PlayerInventory = PlayerInventory();
That should make the class accessible from your other scripts.
The second problem is that you’ve spelled “G__r__enades” wrong!