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 /
  • Help Room /
avatar image
0
Question by Kiimlee · Jan 12, 2017 at 05:40 PM · animation2dmovement

Animator setBool NullReference exception while trying to do animation when tap a button

Hi guys,

I really need help for this so I have a GameObject (Character) link to to a script (CharacterAnimateAction) with an animator inside it. I also have another Battle script (BattleScript) which inside there is the function Attack(). What I'm trying to do is to init and call the animation in BattleCript from CharacterAnimateAction script but I can't make it work, the line anime.setBool("isAttacking", true) is throwing an NullReferenceException. And all of this to try to move my character position to the monster position and do the Attack animation. Can you help me out guys?

namespace Assets.StreetRPG_Core.Battle_View { public class BattleScripts : MonoBehaviour { public Battle battle; public CharacterAnimateAction characterAnimate;

     void Start()
     {
         battle = new Battle();
         characterAnimate = new CharacterAnimateAction ();
     }

     void Update()
     {
     }

     public void PlayerAttack()
     {
         battle.game.Attack ();
         characterAnimate.getCharactersPositions ();
         characterAnimate.AttackAnimation ();
         print (battle.game.getUser().skill1["name"]);
     }

}

public class CharacterAnimateAction : MonoBehaviour {

 // Use this for initialization
 public Vector3 characterPosition;
 public Vector3 monsterPosition;
 public MonsterAnimateAnimation monsterAnimate;    
 Animator anime;

 // Use this for initialization
 void Start () {
     anime = GetComponent<Animator> ();
     monsterAnimate = new MonsterAnimateAnimation ();
     characterPosition = transform.position;
 }

 // Update is called once per frame
 void Update () {
     
 }

 public void getCharactersPositions()
 {
     GameObject theCharacter = GameObject.Find ("Character");
     CharacterAnimateAction characterAnimate = theCharacter.GetComponent<CharacterAnimateAction> ();
     characterPosition = characterAnimate.characterPosition;

     GameObject theMonster = GameObject.Find ("Boss");
     monsterAnimate = theMonster.GetComponent<MonsterAnimateAnimation> ();
     monsterPosition = monsterAnimate.monsterPosition;

     characterPosition = new Vector2 (monsterPosition.x, monsterPosition.y);
 }

 public void AttackAnimation()
 {
     anime.SetBool ("isAttacking", true);
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Pengocat · Jan 13, 2017 at 03:28 AM

You don't need to use new to initialize a Monobehavior script. Instead you should use GetComponent<> to get the reference. The problem you have is that the variable characterAnimate reference is not found yet in the class BattleScripts . Your BattleScripts Start() Method should look something like this.

     void Start()
     {
         characterAnimate = GameObject.Find("Character").
             GetComponent<CharacterAnimateAction>();
     }
Comment
Add comment · Show 3 · 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 Kiimlee · Jan 13, 2017 at 08:46 AM 0
Share

Hi thank you it worked that way! The animation attack launch successfully thank you so much! I'm also trying to move my character game object to my monster game object:

  characterPosition = new Vector2 (monsterPosition.x, monsterPosition.y);


it worked but on the screen the character game object didn't move and stay at the same position. Can you help me with this please?

avatar image Pengocat Kiimlee · Jan 13, 2017 at 03:23 PM 0
Share

The character position variable should be of type Transform and not Vector3 if you want to use it like that.

  public Transform characterPosition;


And then you set it by...

 characterPosition.position = characterAnimate.characterPosition;

In fact you may end up setting the same target to it's own value? It is a bit confusing the way you have things organised.

avatar image Kiimlee Pengocat · Jan 14, 2017 at 09:28 AM 0
Share

Thank you for your reply! I just find out that characterPosition = transform.position doesn't work when we change characterPosition. But when I'm changing directly transform.position value it works!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

198 People are following this question.

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

Related Questions

Object reference not set to an instance of an object (Unity 2D animation) 1 Answer

8-way 2D top down movement (diagonal) idle animation issues 0 Answers

2D animation transitions and unexpected position changes 2 Answers

2D Animation Delay 3 Answers

Simple 2D movement with animation while you are moving 0 Answers


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