Baffled by class, setting variables at Start

The following does not assign the variable values when the class is instantiated (that’s what I think I’m doing anyway). The only way to set the variables of a “newPerson” I’ve made is to wait and do it in a coroutine. I have NO IDEA why this is the case.

I made a JS “Person” class (correctly named Person):

class Person{
	var firstName : String;
	var gender : String;
	var age : int;
	function Person(theName, theGender, theAge){
		firstName = theName;
		gender = theGender;
		age = theAge;
	}
}

I then made a script that I put on an empty GameObject that creates a newPerson and attempts to set that newPerson’s values using a constructor (as seen in the class above). BUT, the variables are not assigned a value until the coroutine kicks on. I can tell because when I run it, the console prints the "name: " part with no value after it until the .5 wait time. Help… please.

var newPerson : Person;

function Start () {
	var newPerson : Person = new Person("claire", "female", 5);
	StartCoroutine(InitializeWait(.5));

}

function Update(){
	Debug.Log("name: "+newPerson.firstName);
}

function InitializeWait(waitTime : float){//------this stuff MUST be done after a few frames for some reason
	yield WaitForSeconds(waitTime);
	
	newPerson.gender = "female";
	newPerson.firstName = "claire";
	newPerson.age = 5;

	//do i need this?
	StopCoroutine("InitializeWait");
}

Uhhh… this is very weird to me. You should be able to work with your Person object immediately. Maybe there’s other code affecting this?

EDIT: Where you assign “newPerson” to? You should have it as a class member:

var newPerson : Person;

function Start () {
	this.newPerson = new Person("claire", "female", 5);
}

function Update(){
	Debug.Log("name: "+ this.newPerson.firstName);
}

This might be the heart of my problem… I don’t understand your question. I thought that by declaring the variable at the top:

var newPerson : Person;

I could then use it from anywhere else in the script (like in the Update function). So trying to answer your question makes me want to re-word the question… where am I assigning the instance of the class Person? I’m assigning it to the variable newPerson In other words, newPerson, is now a variable that holds one instance of the Person Class, and all the variables that are within that class (like age, gender, etc) and should have been set from the construction function of the class. Is this incorrect thinking? I’ve attached a package that includes the two scripts and the scene with the empty game object. There is nothing else going on. Adding the coroutine was the only way I could get the Debug.Log to show any values in the newPerson variables.

437475–15197–$classTest.unitypackage (4.35 KB)

looking at your first code , you are actually recreating a new object Person in start , which may exist only in your start fct , the coroutine I guess take care of the Person Member that you create in top of your script.

Mean that in your update you are not debugging the Person you created in Start() cause it should not exist anymore, if the variable scope rule work same as any other fct in Start()

Fizixman script should perfectly work as you expect btw.

A slow dawning light…giyomu makes it official, I don’t understand the workings of this as well as I thought. Let me see if it’s sinking in yet.

when I put this in the Start function:
var newPerson : Person = new Person(“claire”, “female”, 5);
You’re saying this is wiping out the declaration I made at the top and making it a variable useful to the start function only, not setting it, like I thought. I should have used the following instead:
this.newPerson = new Person(“claire”, “female”, 5);
Which references the already declared object Person. The word “this” is hard to track down in help. Does it mean “the already declared variable”?

Not quite. It’s not wiping anything out, but it only exists in the Start function.

“this” is only useful if you have two variables with the same name, one local and one global. In that case, “this” refers to the global version. Otherwise you can leave it out.

var foo = "global";

function Start () {
    var foo = "local"; // local to Start only!
    print (this.foo + " / " + foo);  
}

That will print “global / local”.

By the way, you should declare the types in your class constructor, unless there’s a good reason not to.

	function Person(theName : String, theGender : String, theAge : int){

–Eric

You guys are so cool. That helped a lot, thank you. I’m hijacking my own thread to take this further. I wanted to see if I could create an array of class objects (like the ones you just taught me how to make). I can’t get Add to work as I think it should. I resorted to implicitly setting each index of the array to get it to work. The bottom two lines that are remarked out are the basis for the question. Why doesn’t Add work there?

var familyArray : Person[];

function Start () {
	peopleArray = GameObject.FindGameObjectsWithTag ("people");
	familyArray = new Person[peopleArray.Length];

	for (i=0; i<peopleArray.length; i++) {
		//I have to use the following
		familyArray[i] = new Person(peopleArray[i].name, peopleArray[i].transform.localScale);

		//instead of using Add, which throws this error:
		//Assets/makePersonArray.js(13,29): BCE0019: 'Add' is not a member of 'Person[]'. 

		//newPerson = new Person(peopleArray[i].name, peopleArray[i].transform.localScale);
		//familyArray.Add(newPerson);
	}
}

function OnGUI () {
	GUI.Label (Rect (5, 20, 200, 25),familyArray[0].firstName);
}

concerning declaring types in my class constructor: Done, good tip, since that’s required when I start learning how to use the pragma stuff, correct?.

Haha, woops. I would have said all the stuff above (that you guys said better anyway), but I didn’t realize I scrolled down your code. I never saw your class declaration of newPerson; only saw the local declaration. :stuck_out_tongue:

EDIT: Arrays are fixed length. You’re better off using System.Collections.Generic.List instead; not sure on the syntax for UnityScript though.

“Add” only works with dynamic arrays. Your Person array is fixed-length.

–Eric