Problem with the this keyword

Hi, i was implementing a class and the this keyword is not working as i expected.

I had this class:

class Unit
{}

that has this constructor:

function Unit (go : GameObject)
{
   go.GetComponent (UnitNeutral).SetInfo (this);
}

but the this keyword is always null, i checked with this:

if ( this == null ) 
{
   Debug.Log ("Null");
}

and it always printed null!

What i want is the game object to had a reference to the Unit class that is the one that controls that game object so i can cycle from the game object to the unit class to the game object.

Im doing something wrong ?

Is the this keyword not valid in that class ?

thanks

.ORG

How about getting an instance of the Unit class like this?

function Unit (go : GameObject)
{
   go.GetComponent (UnitNeutral).SetInfo (gameObject.GetComponent(Unit)); 
}

Hope that works/helps :slight_smile:

Dont worry ill go the other way around…

.ORG