I can’t read your code because it isn’t posted properly (see below), but first guess is that perhaps you’re doing one of the Find/ Get commands? Those are notorious for confusing behaviour.
Remember the first rule of GameObject.Find():
Do not use GameObject.Find();
More information: Regarding GameObject.Find · UnityTipsRedux
More information: Why cant i find the other objects?
In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.
If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.
“Stop playing ‘Where’s GameWaldo’ and drag it in already!”
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: Using code tags properly
- Do not TALK about code without posting it.
- Do NOT post unformatted code.
- Do NOT retype code. Use copy/paste properly using code tags.
- Do NOT post screenshots of code.
- Do NOT post photographs of code.
- Do NOT attach entire scripts to your post. - ONLY post the relevant code, and then refer to it in your discussion.