OK - I’ve been following the Stealth game project example - didn’t really like the way the guy’s been organizing the code - so I thought I could do better - so basically, what I have is:
1- a game object called ‘gameController’ (much like in the tuts) that has:
A) a script attached to it called “GameControllerScript”
B) a number of child objects (you don’t have to know about all of them) - one of them is “music”
2- now ‘music’ has:
A) a script attached to it called “MusicScript”
B) two child objects: NormalMusic, PanicMusic each with NormalMusicScript and PanicMusicScript attached to respectively.
Logically, I made NormalMusicScript and PanicMusicScript inherit from MusicScript, here’s my codes:
public class Music : MonoBehaviour {
public float maxVolume = .8f;
public float minVolume = .1f;
public float fadeSpeed = 2f;
protected GameControllerScript gameController;
protected float targetVolume;
public virtual void Awake()
{
gameController = transform.parent.gameObject.GetComponent<GameControllerScript>();
}
public void FadeTo(float volume)
{
audio.volume = Mathf.Lerp(audio.volume, volume, fadeSpeed * Time.deltaTime);
}
}
public class PanicMusicScript : Music {
void Update()
{
if (gameController.PlayerHasBeenSpotted())
{
targetVolume = maxVolume;
}
else
{
targetVolume = 0;
}
FadeTo(targetVolume);
}
}
(The NormalMusicScript has similar code so no need to post it)
Now the problem is: when I run the game, I get a NullReferenceException at the line
if (gameController.PlayerHasBeenSpotted())
saying that gameController is null - So I debugged to check the problem - I put a breakpoint at the assignment of gameController in the Awake of MusicScript parent class - it got assigned no problem - but then after I stepped, the same Awake of the parent got executed again! - but this time, gameController returned null! - I didn’t know what that was about - so I thought that if I didn’t override the Awake in the child, the Awake of the parent will also get called on the child - so I tried this:
public override void Awake()
{
return;
}
When I debugged again - it didn’t execute Awake twice - but still broke at the same line saying that gameController is null
I tried to use the constructor instead of Awake (cuz I remember having similar problem in the past and managed to solve it that way) but no luck - got some other errors saying not to use the constructor or something…
Why is this happening? - any help would be appreciated - thanks a lot in advance.