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
0
Question by Ralp · Apr 03, 2013 at 11:10 PM · c#inspectornullreferenceexceptionassets

Cannot assign assets to variables via inspector: NullReferenceException

Below is what I am trying to do, play a sound and an animation by pressing fire button (previously assigned on input manager). Please note I use C# language:

 using UnityEngine;
 using System.Collections;
 
 public class ShotgunFire : MonoBehaviour {
     
     public AnimationClip ShotClip;
     public AudioClip ShotSound;
     Animation anim;
     AudioSource audio;
     
     // Use this for initialization
     void Start () 
     {
         anim.AddClip(ShotClip, "fire");
     }
     
     // Update is called once per frame
     void Update () 
     {
         if(Input.GetButton("Fire") == true)
         {
             audio.PlayOneShot(ShotSound);
             anim.Play("fire");
         }
         
     }
 }


The script above was attached as a component to my shotgun model, as shown on the picture below. As you may see, I have assigned assets to all public variables on the script, as requested.

alt text

And when I press play it keeps giving a NullReferenceException error, which is making me fry my brains trying to solve it! Lights please? alt text

problem1.png (5.9 kB)
problem2.png (13.3 kB)
Comment
Add comment · Show 2
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 Benproductions1 · Apr 03, 2013 at 11:41 PM 0
Share

have you defined anim? If not thats your problem

avatar image EliteMossy · Apr 04, 2013 at 01:38 AM 0
Share

@Ralp remember to mark an answer as accepted if it works.

3 Replies

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

Answer by Ralp · Apr 04, 2013 at 03:01 AM

EliteMossy, you forgot to say I had to add the components directly into the object, not associate them to variables in my script. But thanks for your help!

So this is how I solved the problem:

  • Instead of associating the components to variables in script, I added them directly into the object, making the inspector look like this:

alt text

  • Then, by using RequireComponent attributes and unity engine's property variables (in this case audio and animation), I was able to control the object's components by its class. The final code is as follows:

       using UnityEngine;
         using System.Collections;
         
         [RequireComponent(typeof(AudioSource))]
         [RequireComponent(typeof(Animation))]
         public class ShotgunFire : MonoBehaviour {
                 
             // Use this for initialization
             void Start (){}
          
             // Update is called once per frame
             void Update () 
             {
                if(Input.GetButton("Fire") == true)
                {
                    audio.Play();
                 animation.Play();
                }
          
             }
         }
    
    

I am migrating from XNA, where I had to do everything by code. So trust me when I say unity is being a challenging transition (but hopefully worth).


problem1.png (5.6 kB)
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 EliteMossy · Apr 04, 2013 at 04:22 AM 0
Share

$$anonymous$$y code would have added them automatically if you dragged the script on to a GameObject. That is what RequireComponent does.

avatar image
0

Answer by prototype7 · Apr 04, 2013 at 12:12 AM

as Benproductions1 said for instance

 void Start () {
         anim = this.gameObject.AddComponent<Animation>();
         anim.AddClip(ShotClip, "fire");
     }
Comment
Add comment · Show 2 · 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 EliteMossy · Apr 04, 2013 at 12:17 AM 0
Share

No, you never use new for components.

avatar image EliteMossy · Apr 04, 2013 at 01:53 AM 0
Share

I see you edited your answer :D

avatar image
0

Answer by EliteMossy · Apr 04, 2013 at 12:16 AM

you can either put these in Start()

         gameObject.AddComponent<Animation>();
         gameObject.AddComponent<AudioSource>();

then you should be able to

 audio.PlayOneShot(ShotSound);
 animation.Play("fire");


but in all honesty, this is how i would do it

 using UnityEngine;
 using System.Collections;
  
 [RequireComponent(typeof(AudioSource))]
 [RequireComponent(typeof(Animation))]
 public class ShotgunFire : MonoBehaviour {
  
     public AnimationClip ShotClip;
     public AudioClip ShotSound;
  
     // Use this for initialization
     void Start () 
     {
        animation.AddClip(ShotClip, "fire");
     }
  
     // Update is called once per frame
     void Update () 
     {
        if(Input.GetButton("Fire") == true)
        {
          audio.PlayOneShot(ShotSound);
          animation.Play("fire");
        }
  
     }
 }

Good luck :)

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

13 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

Related Questions

Distribute terrain in zones 3 Answers

Inspector cannot find script instance 1 Answer

Boolean is set to true but is unchecked in the inspector 1 Answer

Inspector vs Script: Component best practice? 1 Answer

Multiple Cars not working 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