Search Unity

How to save and load my tower model and its position using Playerprefs

Discussion in 'Scripting' started by quantum_rez, Dec 10, 2012.

  1. quantum_rez

    quantum_rez

    Joined:
    Oct 23, 2012
    Posts:
    35
    Hi all, i'm trying to make game in android devices, my game is tower defense.

    I can use playerprefs to save and load my gold, but when i load it, my tower is lose and i must build again in the other word my tower model and its position is not save.

    I want to ask, how can i use playerprefs to save and load my tower model and its position after i build it?

    Please anyone help me here, i'm really appreciate for your help.

    Thanks :D
     
  2. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    you will have to save the x,y,z (Vector3) of the tower, using separate floats using PlayerPrefs.SetFloat
    for example
    PlayerPrefs.SetFloat("TowerX", 10.0);
    PlayerPrefs.SetFloat("TowerY", 10.0);
    PlayerPrefs.SetFloat("TowerZ", 10.0);

    and then when you are loading set the Vector3 coordinates of the object by setting x,y,z using PlayerPrefs.GetFloat for each.
     
  3. quantum_rez

    quantum_rez

    Joined:
    Oct 23, 2012
    Posts:
    35
    Sorry, maybe my explanation is not clear enough.
    This is an example :
    - I have 2 free grids for building tower, let's say it grid A(x=10,y=10,z=10) and B(x=20,y=20,z=20).
    - I built tower A into grid A and tower B into grid B.
    - When I load with PlayerPrefs, I want tower A still on grid A and tower B still on grid B.

    So, how do I know that the position (x=10,y=10,z=10) is for tower A and (x=20,y=20,z=20) is for tower B?
    And, can PlayerPrefs save a model or prefab?
    Thanks
     
  4. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    You simply name it as you need TowerAx,TowerAy,TowerAz etc.

    These names can be derived through code.
     
  5. quantum_rez

    quantum_rez

    Joined:
    Oct 23, 2012
    Posts:
    35
    hmm. maybe what i mean is how to save instantiate game object, so when play it again my object is still there, and the problem is i can instantiate many game object, so i must save the position one by one and i think will wasted time to do that, any other way?

    thanks