- Home /
function Start() and class in class
Hi,
I'm new in unity3d and i'm doing something wrong. How to use class in class? I write code like this and i can't initiate class Player with values in Stat class fields.
var player : Player;
class Stat{
var current: int;
var max: int;
}
class Player{
var name: String;
var hp: Stat;
var mana: Stat;
var attack: int;
var defense: int;
function Player(name: String, hp: int, hpMax: int, mana: int, manaMax: int, attack: int, defense: int){
this.name = name;
this.attack = attack;
this.defense = defense;
this.hp.current = hp;
this.hp.max = hpMax;
this.mana.current = mana;
this.mana.max = manaMax;
}
}
function Start(){
player = new Player("PlayerName", 10, 20, 10, 20, 99, 99);
}
How to set values to hp.max, hp.current, mana.max and mana.current? How to do it right?
Answer by Eric5h5 · Jan 04, 2012 at 01:33 PM
You need to create a new instance of the class before you can use it; by default it is null. In the same way that you're creating a new instance of Player when you make the player variable.
Just in case you don't understand this answer, the hp and mana variables are classes. Classes are always reference types so you have to create an instance before you can use it.
so in your Player constructor do
this.hp = new Stat();
this.mana = new Stat();
before you access the variables of these objects.
Another way would be to change the Stat-class into a struct
struct Stat{
var current: int;
var max: int;
}
A struct is a value type so it has always something assigned. It can't be null. It's not a reference to another object, it is made of it's variables
The struct keyword doesn't exist in Unityscript; to make a struct you have a class extend System.ValueType.
Answer by jakovd · Jan 04, 2012 at 02:16 PM
You need to make these variables public or make new methods for changing their values. Take a look at public keyword on wikipedia: http://en.wikibooks.org/wiki/Java_Programming/Keywords/public
This is "basic programming stuff" and you'll probably use this in every script you write.
Yep, it seems you mix up Java with JavaScript which are two different languages. In UnityScript (Unity's Javascript) everything is public by default. In C# (and Java, which is quite similar to C#) everything is private by default.
Answer by Jenzik · Jan 04, 2012 at 03:50 PM
function Start(){
player = new Player(); // You need to Make a new Instance of the class before assigning values to it =)
player = new Player("PlayerName", 10, 20, 10, 20, 99, 99);
}
This doesn't make any sense. You create a Player object with the default constructor and then you create another one (with the overloaded constructor) and throw the first one away.
Your answer
Follow this Question
Related Questions
Parent Class variables in Child Class's Inspector (C#) 0 Answers
Field is never assigned to, and will always have its default value 3 Answers
Collisions and Lists 1 Answer
How can I access this class from another script? 1 Answer
Creating a preset class that can be assigned in the inspector 0 Answers