- Home /
How come I cannot set my prefab game component into a script public variable?
I'm following the 2d Roguelike tutorial. In step 13 adding the sounds, I created a sound manager script in the scene. I then made it a prefab with the script and the 2 audio sources in the as a game component. I then added the SoundManager to my loader script, but I cannot drop in the prefab sound manager game component into the loader. The weird part is when I drag the prefab onto the public script variable in Loader in the Unity UI it highlights as a good link but doesn't update when the mouse is released.
My scripts are the following.
public class Loader : MonoBehaviour
{
public GameManager GameManager;
public SoundManager SoundManager;
static Loader()
{
log4net.Config.BasicConfigurator.Configure(new ConsoleAppender(new SimpleLayout()));
}
private void Awake()
{
if (GameManager.Instance == null && this.GameManager != null)
{
Instantiate(this.GameManager);
}
if (SoundManager.Instance == null && this.SoundManager != null)
{
Instantiate(this.SoundManager);
}
}
public class SoundManager : MonoBehaviour
{
public static SoundManager Instance;
public AudioSource EfxSource;
public AudioSource MusicSource;
public float LowPitchRange = .95f;
public float HighPitchRange = 1.05f;
public void PlaySingle(AudioClip clip)
{
this.EfxSource.clip = clip;
this.EfxSource.Play();
}
public void RandomizeSfc(params AudioClip[] clips)
{
var randomIndex = Random.Range(0, clips.Length);
var randomPitch = Random.Range(this.LowPitchRange, this.HighPitchRange);
this.EfxSource.pitch = randomPitch;
this.EfxSource.clip = clips[randomIndex];
this.EfxSource.Pause();
}
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
else if (Instance != this)
{
Destroy(this.gameObject);
}
DontDestroyOnLoad(this.gameObject);
}
}
Hi, is Sound$$anonymous$$anager nested inside Loader ? (may be only when you copy/pasted it in here). It should not : each class should be in it's own script file with the file name = class name
Answer by dukester · Mar 09, 2015 at 01:33 PM
Not sure that is the reason, but I don't think it's a good idea to give variables the same name as the classes they represent (or any other class for that matter). So
public GameManager GameManager;
public SoundManager SoundManager;
should be:
public GameManager gameManager;
public SoundManager soundManager;
Your answer
Follow this Question
Related Questions
I have a problem whit a script and prefabs 2 Answers
Trying to have my Editor Script save the changed settings when I hit Play 0 Answers
How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers
How can I apply a text variable to a prefab 2 Answers
Script on Prefab is not changeable without changing all other instaniated objects 1 Answer