Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DubstepDragon · Aug 20, 2015 at 03:30 PM · unity 5onguiskin

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! :D

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 \n 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 \n 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 \n 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! :D

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cjdev · Aug 20, 2015 at 07:21 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dudester · Aug 20, 2015 at 08:51 PM

 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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DubstepDragon · Aug 26, 2015 at 07:27 PM 0
Share

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

avatar image dudester · Aug 31, 2015 at 09:18 AM 0
Share

$$anonymous$$eep your reference like so ,

Public Power skinscript;

Then call it by saying skinscript.changeskin();

Hope this helps

avatar image
0

Answer by Mikilo · Aug 31, 2015 at 09:28 AM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dudester · Oct 09, 2015 at 07:29 PM 0
Share

you are correct seems i missed that there nice find.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set the Ongui's GUILayout.BeginArea in a panel??? 0 Answers

Skin issues with Spine 1 Answer

Is there compatibility or anything like Skin Morph targets in Unity? 2 Answers

Player Skins shop Menu 0 Answers

Vuforia simultaneous tracked objects 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges