- Home /
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.
And when I press play it keeps giving a NullReferenceException error, which is making me fry my brains trying to solve it! Lights please?
@Ralp remember to mark an answer as accepted if it works.
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:
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).
$$anonymous$$y code would have added them automatically if you dragged the script on to a GameObject. That is what RequireComponent does.
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");
}
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 :)
Your answer
Follow this Question
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