Changing GameObject value before instantiating, no error but not working :/

I’m trying to have 4 buttons that purchase player “skins”. When they are first activated, it checks if coins are more than 1, then 1 “coin” (currency, stored in PlayerPrefs) is subtracted from the total current amount. The skin is now “purchased”, and should be activated on use again. That is, if it was working correctly… But so far I am getting my player object to spawn with the default skin only, purchasing skins work and it only subtracts coins the first time, but it doesn’t activate them when clicked again. I’m using OnGUI because this project is really old and I don’t wish to use the new GUI system. If you can help me by telling me where I went wrong or if I missed out something, that would be awesome! :smiley:

There are two scripts associated with this issue, one for the shop screen that handles all the GUI operations and the other for the skin management. Here they are:

using UnityEngine;
using System.Collections;

public class ShopGUI : MonoBehaviour {

	public GUISkin blahblahskin;

	// Use this for initialization
	void Start () {
		if(PlayerPrefs.GetInt("skin2") != 1) {
			PlayerPrefs.SetInt("skin2", 0);
		}
		if(PlayerPrefs.GetInt("skin3") != 1) {
			PlayerPrefs.SetInt("skin3", 0);
		}
		if(PlayerPrefs.GetInt("ski4") != 1) {
			PlayerPrefs.SetInt("skin4", 0);
		}
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void OnGUI() {

		GUI.skin = blahblahskin;

		if(GUI.Button(new Rect(Screen.width / 2 - 225, Screen.height / 2 - 50, 100, 100), "Skin 1")) {
			Debug.Log("You own skin 1 already, activating...");
			PlayerPrefs.SetInt("CurrentSkin", 1);
		}


		if(GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 50, 100, 100), "Skin 2 

Cost: 1 Coins")) {
if(PlayerPrefs.GetInt(“skin2”) == 0) {
if(PlayerPrefs.GetInt(“Coins”)>=1) {
PlayerPrefs.SetInt(“Coins”, PlayerPrefs.GetInt(“Coins”) -1);
PlayerPrefs.SetInt(“CurrentSkin”, 2);
PlayerPrefs.SetInt(“skin2”, 1);
} else {
Debug.Log(“Insufficient Coins!”);
}
}
if(PlayerPrefs.GetInt(“skin2”) == 1) {
PlayerPrefs.SetInt(“CurrentSkin”, 2);
Debug.Log(“You own skin 2 already, activating…”);
}
}

		if(GUI.Button(new Rect(Screen.width / 2 + 25, Screen.height / 2 - 50, 100, 100), "Skin 3 

Cost: 1 Coins")) {
if(PlayerPrefs.GetInt(“skin3”) == 0) {
if(PlayerPrefs.GetInt(“Coins”)>=1) {
PlayerPrefs.SetInt(“Coins”, PlayerPrefs.GetInt(“Coins”) -1);
Debug.Log(“You now have skin 3. Use again to activate.”);
PlayerPrefs.SetInt(“skin3”, 1);
} else {
Debug.Log(“Insufficient Coins!”);
}
}
if(PlayerPrefs.GetInt(“skin3”) == 1) {
PlayerPrefs.SetInt(“CurrentSkin”, 3);
Debug.Log(“You own skin 3 already, activating…”);
}
}

		if(GUI.Button(new Rect(Screen.width / 2 + 150, Screen.height / 2 - 50, 100, 100), "Skin 4 

Cost: 1 Coins")) {
if(PlayerPrefs.GetInt(“skin4”) == 0) {
if(PlayerPrefs.GetInt(“Coins”)>=1) {
PlayerPrefs.SetInt(“Coins”, PlayerPrefs.GetInt(“Coins”) -1);
Debug.Log(“You now have skin 4. Use again to activate.”);
PlayerPrefs.SetInt(“skin4”, 1);
} else {
Debug.Log(“Insufficient Coins!”);
}
}
if(PlayerPrefs.GetInt(“skin4”) == 1) {
PlayerPrefs.SetInt(“CurrentSkin”, 4);
Debug.Log(“You own skin 4 already, activating…”);
}
}

		if(GUI.Button(new Rect(Screen.width / 2 + 200, Screen.height / 1.2f, 150, 40), "Back")) {
			Application.LoadLevel("mainMenu");
		}

	}

}

using UnityEngine;
using System.Collections;

public class Power : MonoBehaviour {
	
	public static bool IsImmortal = false;
	public static bool IsShield = false;

	public static GameObject skin1;
	public static GameObject skin2;
	public static GameObject skin3;
	public static GameObject skin4;

	public GameObject skin1ref;
	public GameObject skin2ref;
	public GameObject skin3ref;
	public GameObject skin4ref;

	public static GameObject PlayerObject;
	public Transform PlayerSpawner;

	void Awake() {
		skin1 = skin1ref;
		skin2 = skin2ref;
		skin3 = skin3ref;
		skin4 = skin4ref;
	}

	// Use this for initialization
	void Start () {

		if(PlayerPrefs.GetInt("CurrentSkin") != 1 || PlayerPrefs.GetInt("CurrentSkin") != 2 || PlayerPrefs.GetInt("CurrentSkin") != 3 || PlayerPrefs.GetInt("CurrentSkin") != 4) {
			PlayerPrefs.SetInt("CurrentSkin", 1);
		}

		if(PlayerPrefs.GetInt("CurrentSkin") == 1) {
			PlayerObject = skin1ref;
		}
		
		if(PlayerPrefs.GetInt("CurrentSkin") == 2) {
			PlayerObject = skin2ref;
		}
		
		if(PlayerPrefs.GetInt("CurrentSkin") == 3) {
			PlayerObject = skin3ref;
		}
		
		if(PlayerPrefs.GetInt("CurrentSkin") == 4) {
			PlayerObject = skin4ref;
		}

		
		Instantiate (PlayerObject, PlayerSpawner.position, Quaternion.identity);
	}
	
	// Update is called once per frame
	void Update () {

	}
}

Thanks a lot! :smiley:

The objects are instantiated in the Power script, so after clicking the buttons to change the skin are you restarting the Power script?

2 Answers

2

public void ChangeSkin {

         if(PlayerPrefs.GetInt("CurrentSkin") != 1 || PlayerPrefs.GetInt("CurrentSkin") != 2 || PlayerPrefs.GetInt("CurrentSkin") != 3 || PlayerPrefs.GetInt("CurrentSkin") != 4) {
             PlayerPrefs.SetInt("CurrentSkin", 1);
         }
 
         if(PlayerPrefs.GetInt("CurrentSkin") == 1) {
             PlayerObject = skin1ref;
         }
         
         if(PlayerPrefs.GetInt("CurrentSkin") == 2) {
             PlayerObject = skin2ref;
         }
         
         if(PlayerPrefs.GetInt("CurrentSkin") == 3) {
             PlayerObject = skin3ref;
         }
         
         if(PlayerPrefs.GetInt("CurrentSkin") == 4) {
             PlayerObject = skin4ref;
         }
 
         
         Instantiate (PlayerObject, PlayerSpawner.position, Quaternion.identity);
     }

by using start to instantiate an object it will only work if the player starts the game again after buying the skin , so rather make a function for instantiate so that you can call it after buying the skin, like above.

then call it in OnGui function with a button press.

Thing is, the OnGUI method is in another script as shown above, and I tried referencing it to access it from the other script but failed... horribly xD What's the easiest way I can go about doing that? :D Thanks for your help so far :3

Keep your reference like so , Public Power skinscript; Then call it by saying skinscript.changeskin(); Hope this helps

You never called PlayerPrefs.Save();
Without this call, all your SetInt, SetString, SetFloat, etc… will never be saved.

you are correct seems i missed that there nice find.