Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 07, 2016 at 05:31 PM by HazyCarnation for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by HazyCarnation · Sep 20, 2016 at 06:08 AM · c#animationplayeranimator controller

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HazyCarnation · Sep 20, 2016 at 05:09 PM 0
Share

Thank you very much! I will get right on to it and see if I can make it work! :)

avatar image HazyCarnation · Sep 20, 2016 at 06:04 PM 0
Share

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);
     }
 }

}

avatar image flaviusxvii HazyCarnation · Sep 20, 2016 at 07:21 PM 0
Share

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);
     }
 
    
 }

avatar image HazyCarnation flaviusxvii · Sep 21, 2016 at 06:41 PM 0
Share

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!

Show more comments
avatar image
0

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 { }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges