简体   繁体   中英

Object Reference not set to an instance of an object while play sound using script in unity c#

I want to play and pause sound in game using script. I have a sound manager script to load and play sound file. I try to play audio from other script. But the error occurs saying " Object Reference Not set to an instance of an object " on the other script where I have written code to play audio.

sound manager script

using UnityEngine;
using System.Collections;

public class soundManager : MonoBehaviour {

    public static AudioSource au_GameStart;
    public static AudioSource au_afterStart;
    public static AudioSource au_carStart;
    public static AudioSource au_go;
    public static AudioSource au_strike;

    public static bool muteStatus;
    public static soundManager instance;
    void Start () {
        muteStatus = false;
        instance=this;
        au_GameStart = (AudioSource)gameObject.AddComponent <AudioSource>();
        au_afterStart = (AudioSource)gameObject.AddComponent <AudioSource>();
        au_carStart = (AudioSource)gameObject.AddComponent <AudioSource>();
        au_go = (AudioSource)gameObject.AddComponent <AudioSource>();
        au_strike = (AudioSource)gameObject.AddComponent <AudioSource>();

        AudioClip clip_GameStart;
        clip_GameStart = (AudioClip)Resources.Load ("SFX/game_start");
        au_GameStart.clip = clip_GameStart;
        au_GameStart.loop = false;

        AudioClip clip_AfterStart;
        clip_AfterStart = (AudioClip)Resources.Load ("SFX/after_start");
        au_afterStart.clip = clip_AfterStart;
        au_afterStart.loop = false;

        AudioClip clip_carStart;
        clip_carStart = (AudioClip)Resources.Load ("SFX/car_start");
        au_carStart.clip = clip_carStart;
        au_carStart.loop = false;

        AudioClip clip_GO;
        clip_GO = (AudioClip)Resources.Load ("SFX/go");
        au_go.clip = clip_GO;
        au_go.loop = false;

        AudioClip clip_Strike;
        clip_Strike = (AudioClip)Resources.Load ("SFX/strike");
        au_strike.clip = clip_Strike;
        au_strike.loop = true;
}

other script

void Start()
{
soundManager.au_GameStart.Play();
}

When I run unity game , it give an error in other script saying "Object Reference not set to an instance of an object ". I have attached soundmanager script to gameobject named SoundManager . And SoundManager gameobject loads sound during play mode.

Because Start() in otherscript is executed before Start() in soundManager . Changing Start() in soundManager class to Awake() should fix the problem.

Or

If you didn't want renaming you could change Script Execution Order

Update: As Everts pointed out in his comments, doing initialization in Start() or Awake is commonly not a good practice because their execution order cannot be guaranteed unless you explicitly specify Script Execution Order . But even Script Execution Order can lead to hard to understand code because Script Execution Order itself is not visible in code.

So the idea is that, you create an Init() so you have full control over initialization of that object. And if in side the Init() you need to init some other objects, call Init() of them.

EDIT : When you get to a point where one class (ClassA) depends on another (ClassB), it becomes necessary that ClassA triggers ClassB.

public class ClassB : MonoBehaviour
{
    [SerializeField] private MonoA monoA = null;
    public void Init(){
        this.monoA = this.gameObject.GetComponent<MonoA>();
    }
    public void GetMonoAValue(){
        if(this.monoA == null){  this.monoA = this.gameObject.GetComponent<MonoA>(); }
        return this.monoA.Value;
    }
}

public class ClassA:MonoBehaviour
{
     private void Start(){
         ClassB classB = FindObjectOfType<ClassB>();
         classB.Init();
         Value v = classB.GetMonoAValue(); 
     }
}

This case is fairly simple, you could directly call the GetMonoAValue since it initializes in case of null value. This was for the example and your Init method may contain more code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM