How to store a change in prefab during playing?

Well, If question a bit confusing let me explain clearly. I was trying to make a really basic upgrade menu after every level for each defender. So players have their own fate in their hands since after every level they decide which defender they want to upgrade as you see in the below.

In order to do what I actually want I draw new defenders for example archer and took archer’s prefab changed sprites,health,name etc. and saved as a new character.

pic1

After that, I wrote a script similar to buttonScript which I give people a desicion as I screenshot on above.
What I did in script, when player select one symbol changed other’s color and disabled their colliders.

Now player selected to upgrade wheat>>sprite changed to new one

Since selected defenders have different prefab,fdff in code I changed also the prefab that buttons hold with buttonscript.

 public GameObject defenderPrefab;  
    public Sprite upgradedSprite;
    public bool hocabitir = false;

    private int krema = 0;
    private upgradeButton[] buttonArray;
    private ButtonScript[] buttons;
    private gameTimer gametimer;
   
void Start () {
        buttonArray = GameObject.FindObjectsOfType<upgradeButton>();
        buttons =   GameObject.FindObjectsOfType<ButtonScript>();
        gametimer = GameObject.FindObjectOfType<gameTimer>();
    }
        {
            if (this.name == buton.name && krema<=1)
            {
             buton.GetComponent<ButtonScript>().defenderPrefab = defenderPrefab;
             buton.GetComponent<BoxCollider2D>().isTrigger = false;
                krema = krema + 1;
             hocabitir = true;
                    gametimer.idEndOfLevel2 = true;             
  }
}

I put the code here to show just how I changed the Buttons GameObjects defenderPrefab; Lets look at in unity the results of this change:
sdfdsf

fdff

So at the end I changed something in the scene just before the nextlevel however It doesnt apply to prefab since I dont know how to apply changes to prefab in runtime. With this new gameObject I want to take this information and use it in the next level. How should I achieve my goal ? If you read until here and have no solution at least thanks for the patience =) Have a good day!

Hi Ege,

If I understand correctly, you have a series of prefabs which are for each state of an upgrade process for a character.

During the course of a level the player upgrades and you would like to reflect the upgrade in the buttons on the screen.

The problem is then that they are not persisted when the next scene loads.

The clue here is in that last sentence, persistence.

You could consider not destroying the buttons on load, that might work, but I would suggest having some form of system in place that is maintaining the state of the game at any time. At any time during the game you should be able to make a call to that code to determine which level the player is at (from an upgrade perspective) and when a scene loads the various components can make calls to this system in order to get the information they need. The button scripts just needs to be told which set of prefabs to display.

Consider perhaps one game object, which isn’t destroyed on load, uses a singleton pattern within a script component and has some fields to store the data you need to maintain.

Hope this helps :slight_smile:

2 Likes

Hi Rob thanks for the solution,

I just tried the same thing with DontDestroyOnLoad() a few hour ago and it worked. I was planning to write here but you were faster and know more than me =) Now I will look what is singleton pattern thanks for the advice. You are the best in this community and always give me the right answer :sunglasses:

1 Like

Hi Ege,

You’re very welcome, and note, its “a” solution as opposed to perhaps “the” solution, there are always many ways you can achieve things, some more complicate than others.

I am looking forward to seeing a video of your game at some point, I have seen you post several times with some sprites from your version of Glitch Garden so it would be great to see it all coming together.

Thank you for the compliment, very kind, I do what I can :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms