2
votes

I have script (#2) below with a public AudioClip variable. When I 'addComponent', it loses that reference.

I've tested manually adding it to an object in the editor, and in that case it works OK. Why does my script added during runtime lose the reference?

GameObject 1: has this script (#1) attached

void HitByRay() {
    clock = GameObject.FindGameObjectWithTag("Clock_Arrow").GetComponent<Clock>();
    clock.gameObject.AddComponent<Tick_Audio_Script>();
}

Which attaches the following script (#2) to the 'Clock' object.

public int safetyCounter;
float gapToNext;
public AudioClip tickAudio;

// Use this for initialization
void Start () {
    startTicker(100);
}

void startTicker(int maxTicks)
{
    safetyCounter = maxTicks;
    gapToNext = 1f;
    playTick();

}

void playTick()
{
    Debug.Log("Tick");
    if (gapToNext < 0.1 || safetyCounter == 0)
    {
        Debug.Log("We're done...!");
        return;
    }
// **ERROR HERE CLIP NOT FOUND** 
    AudioSource.PlayClipAtPoint(tickAudio, gameObject.transform.position, 1f);

    gapToNext = gapToNext * 0.97f;
    safetyCounter--;
    Invoke("playTick",gapToNext);
}

Here's the script in the editor, where I've assigned the audioclip. In the Editor

But when it's attached via 'AddComponent', the reference to that clip does not come through (after I hit play and 'hit' my trigger object which attaches this script)? This results in a null reference error as there is no clip found to be played.

After addComponent

My AudioListener (located on a different object) is working, as there are other sounds being played correctly in the scene.

Again, I've tested adding this script manually to any object pre-run in the editor it works. Why is this?

1
Hi MBKA. You make it difficult by not including the full code, because, I can't see what the hell the classes are called and so on. Apart from that please read this: are you saying that it does't appear IN THE EDITOR, WHEN YOU ARE JUST "SITTING THERE" BETWEEN PLAYING? if so that's correct. only once you actually Play! will it run the code and find the value. - Fattie
dude you DEFINITELY should not name it Tick_Audio_Script. It's just TickAudio.cs. Never use underscores, for any reason ever. And never put "script" in a script name. - Fattie
That is the full code for now, though I plan to add more to script #1 later (do other stuff when hit). I've placed the actual startTicker in a separate script as various interactions may trigger that. To clarify - the first image is the script in my project window. Then 2nd image is after I've hit 'play' and 'hit' the trigger object which attaches TickAudio script - this results in a null reference error (as the script didn't keep the reference and has no audioclip to play). Hope that clears it up ! - mbka
Point taken on naming convention ! :) - mbka
its not the full code, you didn't include "Class ... blah" etc. Anyway no worries. - Fattie

1 Answers

1
votes

This has a simple solution. You can do one of the following:

1) Create a prefab and add it to "clock" with the needed references.

2) do this:

void HitByRay() {
    clock = GameObject.FindGameObjectWithTag("Clock_Arrow").GetComponent<Clock>();
    clock.gameObject.AddComponent<Tick_Audio_Script>();
//NEW PART
    clock.gameObject.GetComponent<Tick_Audio_Script>().TickAudio = (desired Audio);
}

Hope this helps.