Accessing a script from another script in different scene
I've been having a lot of trouble with this. I've attached Script A to Object A in Scene A and I want to access Script A in Script B attached to Object B in Scene B. And yes, I've used DontDestroyOnLoad. It works, but I keep getting null errors because when I try to attach Script A to Script B in the inspector, Script A isn't there. Note: Script A has Boolean values that I wish to be read by Script B.
I'm trying to make a game which involves choices and after you make a choice, you go to the next scene, but you can go back to the previous scene to change your choice. I want the choices to be saved somewhere, so at the end all the choices you picked will be displayed. Can someone help me out here? I know my question is a bit vague, but if someone can help me with my first problem then that would be helpful. I've heard a lot about PlayerPrefs, but it seems a little confusing. I'm not sure I want to go that route right now. I'd rather just stick with the Don'tDestroyOnLoad method for now if I can.
Get the instance of ScriptA in scriptB EX: ScriptA scriptA = GameObject.find(scritpAobject name).GetComponent(); now you can access the scriptA components using scriptA object.
Answer by Nickyj · Aug 24, 2015 at 09:23 AM
Create a dummy gameobject and attach the script you wish to carry forward to next scene to it. Add Don'tDestroyOnLoad to your script. Now choice is yours either make that script singleton or create static variables. Now make a prefab out of that dummy gameobject. You can now add this prefab to all the scenes you want that script and access that variable using class name directly if you go with static variable.
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad not working after changing parent temporary 0 Answers
How to keep selected player active and all others inactive when new scene loaded 3 Answers
Saving highscore for multiple scenes 1 Answer
How to load a scene for the second time with a DontDestroyOnLoad object. 1 Answer
Destroy object with dontdestroyonload 2 Answers