Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by BozoBill · Jun 13, 2015 at 12:36 PM · animationjavascriptunity5

Trying to get a sword swinging animation to play on mouse button 1 (unity 5)

I'll start out by saying I'm very new to the game dev world. So Im trying to get a simple first person sword swinging animation to play when I press left mouse button in unity 5. Im getting an error that says "UnnasignedReferenceException: The variable sword of melee has not been assigned. Really unsure of what to fix and was wondering if anyone could set me on the right track? Thanks in advance!

 #pragma strict
 
 var Damage : int = 50;
 var Distance : float;
 var MaxDistance : float = 1.5;
 var sword : Transform;
 
 function Update ()
 {
     if (Input.GetButtonDown("Fire1"))
     {
         sword.GetComponent.<Animation>().Play("attack");
         var hit : RaycastHit;
         if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
         {
             Distance = hit.distance;
             if (Distance < MaxDistance)
             {
                 hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
             }
         }
     }
 }

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

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

Answer by Johnz1234 · Jun 13, 2015 at 05:27 PM

   function Start(){

   anim = GetComponent<Animator>();
   //I don't know for sure how to write in here because iam C# user I know something about java but this shoud get you started.
   } 
   function Update ()
  {
      if (Input.GetButtonDown("Fire1"))
      {
          //make an animator put your animations in there - anim = Animator now every time your                      mouse button is down animation will play every time button is up it will stop :D
          anim.SetBool("Attack",true);
      }else{
           anim.SetBool("Attack",false);
          var hit : RaycastHit;
          if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
          {
              Distance = hit.distance;
              if (Distance < MaxDistance)
              {
                  hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
              }
          }
      }
  }
Comment
Add comment · Show 1 · 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 BozoBill · Jun 15, 2015 at 03:55 AM 0
Share

Thanks so much, I think I was in over my head on this anyway but I did get it working after a long time and a little tweaking from both your and mickmorana's answers!

avatar image
1

Answer by mickmarona · Jun 13, 2015 at 07:09 PM

Reference type variables (as opposed to value types like int, float, bool, struct, and so forth, which hold data directly) require you to assign them a reference before they can be used, whether this reference stems from an existing object that you want to manipulate (such as a gameobject in the scene), or a new object that you're creating then and there (such as a new List, for example.) You should be able to hover over any type in your IDE and it'll tell you whether it's a class or a struct; classes are always reference types, whereas structs are always data types.

Transform is a reference type and your sword variable references nothing, which is problematic when you use GetComponent on it--it doesn't know what GameObject you're talking about to get a component from, because it doesn't know what Transform you mean which is attached to that GameObject, thus it throws the UnassignedReferenceException error: as the name states, your reference is unassigned. So let's assign it:

First, write a function named Start(); this is built-in like Update(), and it gets called once when the script component is first activated. Inside this Start() function you've added, write sword = GameObject.Find("x").GetComponent.() to assign a Transform to the sword variable, where "x" is the name of the gameobject in your scene holding the Transform you want to reference. Now the compiler knows what Transform you're talking about when you use the sword variable, and now it can grab the Animation component because it knows what gameobject you're referencing.

Also, declare a variable of type Animation and in the Start() function assign that to sword.GetComponent.(), so that you aren't grabbing it every single time you hit the mouse button; it's faster, tidier and more readable to put all your GetComponent calls in your Start() function and to keep these components in separate variables.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

No animation attached to gameobject? 2 Answers

Simple go forward and turn back loop movement stuck to the limit without reverse 2 Answers

I can't edit my animation or play my other animation 0 Answers

How to control speed of animation on Unity by a .txt file? 1 Answer

Animation doesn't work 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