I am attempting to make a game where a building spawns a Unit with a random personality assigned to each, and it keeps that the entire time it exists. I am unsure what to use to do this.
Firstly: I’m assuming I put the starting variables under function Start.
Secondly: I’m assuming I make each ‘var’ ‘private var’ in order to make then unique to the Unit (hp, hunger, ect).
Thirdly: I found Random.range in the Unity Script Reference, but I’m not exactly sure how I would apply this to my code.
Thanks in advance!
Hello,
private
just means it can’t get accessed from outside of the script, it has nothing to do with that variable’s value or how it works inside of the script. (It’s just a property of the variable. Technically you can still access private variables from outside of the script).
If you want something to be randomised when the script is Instantiated, simply put the randomisation code in either the Start
, which is called just before the first Update
, Or Awake
which is the first things that gets called after the script is instantiated.
Heres an example just to make it more clear:
//This randomises the "value" variable when the object is created
var value:float = 0; //should be between 0 and 100
function Awake() {
value = Random.Range(0, 100);
}
Hope this helps,
Benproductions1