- Home /
The question is answered, right answer was accepted
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?
Answer by Fornoreason1000 · Sep 20, 2016 at 07:25 AM
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) http://answers.unity3d.com/questions/727389/an-object-reference-is-required-to-access-non-stat-7.html
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.
https://msdn.microsoft.com/en-au/library/9kkx3h3c.aspx
https://msdn.microsoft.com/en-us/library/system.object(v=vs.110).aspx http://stackoverflow.com/questions/2219566/how-to-define-an-instance https://msdn.microsoft.com/en-us/library/k6sa6h87.aspx
and here is a C# tutorial on creating instances.
https://www.youtube.com/watch?v=2T4jY_YDpF4
https://www.youtube.com/watch?v=e7Yj6vLyYOI
Hope it helps.
Thank you very much! I will get right on to it and see if I can make it work! :)
Hmm I've rewritten the code but I'm still not sure why I'm getting errors. It now gives me the opposite error "Object reference not set to instance of object" at row 20. But didn't i just set it at row 19? It looks like this:
public class Sword : $$anonymous$$onoBehaviour {
private Vector2 last$$anonymous$$ove;
private Animator anim;
// Use this for initialization
void Start () {
/*PlayerController myPlayerController = GetComponent<PlayerController> ();
myPlayerController.last$$anonymous$$ove.x = GetComponent<PlayerController> ().last$$anonymous$$ove.x;
myPlayerController.last$$anonymous$$ove.y = GetComponent<PlayerController> ().last$$anonymous$$ove.y;*/
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update () {
PlayerController myPlayerController = GetComponent<PlayerController> ();
myPlayerController.last$$anonymous$$ove.x = GetComponent<PlayerController> ().last$$anonymous$$ove.x;
myPlayerController.last$$anonymous$$ove.y = GetComponent<PlayerController> ().last$$anonymous$$ove.y;
anim.SetFloat ("Last$$anonymous$$oveX", last$$anonymous$$ove.x);
anim.SetFloat ("Last$$anonymous$$oveY", last$$anonymous$$ove.y);
if (GetComponent<PlayerController> ().attacking)
{
anim.SetBool ("Sword", true);
}
if (GetComponent<PlayerController> ().attacking == false)
{
anim.SetBool ("Sword", false);
}
}
}
Your code is extremely confusing.. I'm guessing there's a "Player" gameobject with a PlayerController component, and a "Sword" gameobject with a Sword component.
public class Sword: $$anonymous$$onoBehaviour {
public GameObject player;
private PlayerController playerController;
private Animator anim;
void Start() {
playerController = player.GetComponent<PlayerController>();
anim = GetComponent<Animator>();
}
void Update() {
anim.SetFloat("Last$$anonymous$$oveX", playerController.last$$anonymous$$ove.x);
anim.SetFloat("Last$$anonymous$$oveY", playerController.last$$anonymous$$ove.y);
anim.SetBool("Sword", playerController.attacking);
}
}
Oh my god, flaviusxvii, I got no errors! That's a much simpler code than I wrote, I was so lost! You guessed right! I should probably have clarified that. The animator doesn't seem to react to the parameters for some reason though. It's as if the parameters doesn't know where to get their values, but playerController = player.GetComponent(); specifies that, right? Also thank you very much, Fornoreason1000, yes I think that's what the game was saying. "Null reference exception" or something. I added the line to my script, it seems very useful in the future!
Answer by UNDERHILL · Sep 21, 2016 at 03:52 PM
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 { }
Follow this Question
Related Questions
How to prevent an animation from triggering more than on due to fast clicking? 2 Answers
What would be the best way to do something based on the animation state of the character? 0 Answers
why I have to anim.getComponent in update() function when I had done in Start () function 2 Answers
Problems with player movement animation 0 Answers
How to trigger different animations depending on where a gameobject collides? 1 Answer