1
\$\begingroup\$

I am making a really simple game where you are a dot and you go around and shoot other dots. I have it set up so in the start menu you can spend points to upgrade your dots stats. (ie. make it shoot faster, move faster.) I am using a prefab NumberHolder object with a script on it to hold the numbers between scenes and I am having an issue. If I have the NumberHolder selected in the inspector the game works perfectly, but if I don't have it selected the numbers all change like normal while I am on the scene that adjusts the numbers, but when I go to the next scene which is the game the numbers seem to have a 50% of not updating to the player. Furthermore, if I select the NumberHolder at this point the numbers are all still at their starting stats. I used to have the NumberHolder located 3 folders deep, Assets/Prefabs/StatHolders and it didn't work at all unless it was selected. I have since moved it up to the Prefabs folder and it now works around 50% of the time. Do I have it to deep? If so is there a way to work with it that deep because I want to try and stay organized or is there a whole better way of doing what I am attempting to do here?

I am using Unity 5.4.0f3 and coding in C#.

\$\endgroup\$
4
  • \$\begingroup\$ How do you link the NumberHolder to the rest of your code? \$\endgroup\$ Commented Oct 25, 2016 at 14:57
  • \$\begingroup\$ I create a object and add a NumberHolder script to it that has a bunch of public floats that are adjustable by buttons before the game starts. I than turned that object into a prefab and deleted the original object. I get the numbers with my PlayerController script using GetComponent<> and pass a few variables to the projectiles through the player controller script. \$\endgroup\$ Commented Oct 25, 2016 at 15:22
  • \$\begingroup\$ I attempted an answer, but not 100% sure I understood what you're doing. Are you modifying the prefab outside the scene at runtime? (if so, how?) \$\endgroup\$ Commented Oct 25, 2016 at 16:36
  • \$\begingroup\$ I am trying to modify the prefabs in the game. Before the game you go through a few menu scenes where you select all your stats. Those are menu scenes before the actual game, and I don't have the objects that access those numbers in those scenes. I am very new to coding, so I'm going to do some research on some of your solutions, they look like they mite work. \$\endgroup\$ Commented Oct 25, 2016 at 16:55

1 Answer 1

0
\$\begingroup\$

Prefabs are templates, the prefabs outside of a scene are not supposed to change at runtime. If you need to save player data between scenes there's a variety of options:

  • Set a gameobject as don't destroy on load - it will persist between scene changes
  • Use static variables
  • Create a non-monobehaviour singleton
  • Save data to playerprefs
  • Use additive loading instead, so game objects naturally persist between scenes
\$\endgroup\$
6
  • \$\begingroup\$ Are you not supposed to modify prefabs in the game? The reason I ask this is because I wasn't able to access the game objects from the prefabs, but I can access the prefabs from the game objects so I found a solution to pass the numbers along. Sorry if that doesn't make since, I am new to coding. Some of these answers look like they mite work so I am going to check them out. \$\endgroup\$ Commented Oct 25, 2016 at 16:59
  • \$\begingroup\$ So, looking up static variables, it looks promising. If I change a game object stats using static variables, if I instantiate that object in another scene will those variables follow? \$\endgroup\$ Commented Oct 25, 2016 at 17:19
  • \$\begingroup\$ Yes, those variables will follow. \$\endgroup\$ Commented Oct 25, 2016 at 17:45
  • \$\begingroup\$ I think modifying the prefab in your project folders from scripts is something that might kinda work in the editor, but not from a built game. I think it should be avoided as a way of keeping player data. Modifying the prefabs once they have been added to the scene as gameobjects is fine (but changes to them won't persist). \$\endgroup\$ Commented Oct 25, 2016 at 19:30
  • \$\begingroup\$ Sorry to bother you but I have one more question on this. I set the object as don't destroy on load and it is staying through all the scenes. I don't know how to reference this object in a future scene to get the numbers from it because it is not in the current scene. \$\endgroup\$ Commented Oct 26, 2016 at 17:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.