Why doesn't it get my values?

I edited the code from FlaviusXVII:

public class Sword: MonoBehaviour {

public PlayerController playerController;
private Animator anim;
public Vector2 lastMove;

void Start() {
	anim = GetComponent<Animator> ();
}

void Update() {
	if (playerController.lastMove == null) {

		print("lastmove is null");
	}
	lastMove.y = playerController.lastMove.y;
	lastMove.x = playerController.lastMove.x;
	anim.SetFloat ("LastMoveX", lastMove.x);
	anim.SetFloat ("LastMoveY", lastMove.y);
	anim.SetBool ("Sword", playerController.attacking);
}

}

The code is getting the variables as I want them, as I can see the numbers changing in Unity. I can’t see the numbers changing in the animator for some reason though, even though this is how I have applied variables to the animator before. What am I doing wrong?

That Error sounds like your trying to call an Instance method without a an instance of the Object. In other words, the compiler doesn’t know which instance that member belongs to. it doesn’t even know if there is an Instance!. Objects references and instances of Object types is a fundamental of OOP programming languages.

basically you need to make an instance or find one of PlayerController before you can try to do anything with lastmove.

This Question does the same with the Transofrm class. he uses Transfrom which is a type. rather than transform which is a object reference(aka an instance of Transform)

creating instances is very simple, its just like this.

public class MyClass {

public void CreateSomeInstance() {
PlayerController myPlayerController = new PlayerController();

}    
}

if PlayerController happens to inherit from MonoBehaviour(e.g it attached to a GameObject)
The next example is more accurate. Monoehaviour instances can be found, they get created when the GO is created. you just have to find it.

public class MyClass : MonoBehaviour {

public void Start() {
PlayerController myPlayerController = GetComponent<MyPlayerController>();

}


}

if you wish to read more on Object instances you should check out the MDSN site. it mostly features the .NET4 framework.

http://stackoverflow.com/questions/2219566/how-to-define-an-instance

and here is a C# tutorial on creating instances.

Hope it helps.

It is important to understand the difference between a type/class and an instance of a type (which is an object).

C# is a strongly typed language. What is a “Class” is referred to as a “Type” in C# and it literally is a type of any instantiated ‘object’. An understanding of inheritance and polymorphism will help you understand why. Unity has decent tutorials on this.

So say you have the definition of a class/type

public class MyType
{
  public bool myBool;
}

This definition itself is a TYPE. It cannot itself be changed or have variables set on it!

You cannot set the properties on a type as such; (which is what you are trying to do)

MyType.myBool = false;

What we can now do however is create an INSTANCE of the type. In this case say it is a GameObject,

GameObject myNewInstanceOfAType = new MyType();

now we can set the value on the instance of the type (our new Object called myNewInstanceOfAType)
(An “object” is literally an instance of a type)

myNewInstanceOfAtype.myBool = true;

So you must think in terms of types. Everything in C# is strongly typed. Everything must have a type that is known at runtime - inheritance helps you structure around this.

This is good because you can also now reuse your type and it retains any variables and functions that it had in the newly instantiated object

   GameObject anotherNewInstanceOfAType = new MyType();
   anotherNewInstanceOfAType.myBool = false;

remember that in Unity at the bottom level if it is a script to be attached to a GameObject it needs to ultimately inherit from monoBehaviour (meaning you want to be able to AddComponent(MyCustomType) it has to ultimately inherit from a class which inherits monobehaviour)

public class MyType : MyBaseType
{
  public bool myBool;
}

public class MyBaseType : MonoBehaviour { }