- Home /
start method does not start quick enough?
When i put a coroutine function at the bottom of a start method, while assigning variables from top to bottom, i get an error. But how could this be possible since all the variables are assigned already? Does unity take extra time to call those variables? I am working in HDRP, either this might be the case, or not. Anyways, Here is the portion of a script i made that causes this variable (It's a draw function, i cannot just say 'if audiosource == null)' otherwise the sound will not play, witch is a huge issue since this is the beginning of the game.
public void Start()
{
gunAudio = GetComponent<AudioSource>();
StartCoroutine(DrawWeapon());
}
public IEnumerator DrawWeapon()
{
if (!Drawing)
{
// anim.CrossFadeQueued(animDraw.name, 0.08f, QueueMode.PlayNow);
if (EquipSound != null)
{
gunAudio.PlayOneShot(EquipSound);
}
// Drawing = true;
// yield return new WaitForSeconds(EquipTime); (1 second)
// Drawing = false;
}
}
Would this be something i did wrong?Error: The variable of gunAudio of GunScript has not been assigned.
Alternatively to the other comments, you could also use
yield return new WaitForEndOfFrame();
before the rest of your DrawWeapon function to make sure it gets called, well, at the end of the frame.
Answer by $$anonymous$$ · Nov 24, 2018 at 03:03 AM
To be honest, I usually go to GetComponent() in Awake(), Always.
Answer by Bunny83 · Nov 23, 2018 at 09:31 PM
Are you sure that there is an AudioSource component attached to the same gameobject as this script is attached to? My guess is there isn't such a component and therefore GetComponent returns null.
If there is a component attached to the same object you can be sure that the assignment has completed before your coroutine even starts. Maybe there's something in a different script which manipulates the variable of this script. Also note that the coroutine as you have posted it would not compile since it currently does not contain any yield statement.
Im sure, i put a '[RequireComponent(typeof(AudioSource])' for it
"RequireComponent" is irrelevant if you added it after you attached the script. "RequireComponent" does not ensure the component is attached. It's just an attribute used by the editor at the moment you attach the script through the editor. Any other means of adding the component will not add the components specified in the attribute. So are you sure that the component is there? Also keep in $$anonymous$$d it has to be on the exact same gameobject. Child or parent object do not count.
Try this:
public void Start()
{
gunAudio = GetComponent<AudioSource>();
if (gunAudio == null)
Debug.LogWarning("No AudioSource component found on this gameobject);
StartCoroutine(DrawWeapon());
}
If you see this warning you don't have an AudioSource component on your gameobject.
I did not get that warning, the error still occurs. And an audio source is always attached to an object. I have no idea what could cause the problem, i can record a video on it if you want
Answer by EhanGoosch · Feb 22, 2019 at 01:34 AM
I know that this question is solved, but it might also help if you go into your project settings and set this script to be the first script executed. (In the script execution order tab)
Your answer
Follow this Question
Related Questions
Initialising List array for use in a custom Editor 1 Answer
How to stop a co-routine in C# instantly? 5 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Coroutines and states 1 Answer