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 LeoSaurusRex · Apr 15, 2016 at 04:42 AM · c#instantiatearrayplayerprefsprefab-instance

how can I improve my "character select" script?

so i made this very archaic and hard-coded "character select" system, it consist of saving the id of the character "skin" (which the player changes with the key arrows to match the visual representation) to the player prefs, so then in-game, the player character instantiates this "skin" at start reading the int saved previously and comparing it to a game object array (arranged by hand) also, the ui displays the current skin loaded like "current player: cat-lord" by manually setting what the int saved in player prefs is equals regarding the name. now, it works, and I'm pretty proud of it, buuuuuut... what will happen when 3 skins turn into 10? then 20??? I'm wondering if there is anyone among you who can walk me or point me to the right direction on how i should go about automating some of this process (if at all possible) or if there is a better method. thanks for reading!!! I know it's a lot to ask, but even if you know a small way to ease the load of setting everything by hand, it'd be much appreciated.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class Showroom_Manager : MonoBehaviour {
     public GUISkin skin;
     public string ID;
 
     public Vector3 distanceAmount;
     public bool keyDown = false;
 
     //shells
     public int shellQuantity = 2;
     public int currentShell = 0;
 
     void Start()
     {
         PlayerPrefs.SetInt ("Choosen Shell", 0);
     }
 
     void Update ()
     {
         //right arrow command
         if (Input.GetKeyDown (KeyCode.RightArrow))
         {
             if (keyDown == false && currentShell <= shellQuantity -1 ) 
             {
                 transform.position += -distanceAmount;
                 keyDown = true;
                 currentShell += 1;
             }
         }    
         if (Input.GetKeyUp (KeyCode.RightArrow)) {
             keyDown = false;
         }
 
         //left arrow command
         if (Input.GetKeyDown (KeyCode.LeftArrow))
         {
             if (keyDown == false && currentShell >= 1) 
             {
                 transform.position += distanceAmount;
                 keyDown = true;
                 currentShell -= 1;
             }
         }    
         if (Input.GetKeyUp (KeyCode.LeftArrow)) {
             keyDown = false;
         }
 
         //enter command
         if (Input.GetKeyDown (KeyCode.Return))
         {
             if (keyDown == false) 
             {
                 keyDown = true;
                 PlayerPrefs.SetInt ("Choosen Shell", currentShell);
                 SceneManager.LoadScene("main menu");
             }
         }    
         if (Input.GetKeyUp (KeyCode.Return)) {
                         keyDown = false;
                     }
 
         //manually setting the name displayed
         PlayerPrefs.GetInt ("Choosen Shell");
         if (currentShell == 0) {
             ID = "No-Shell";
         }
 
         if (currentShell == 1) {
             ID = "Spark";
         }
 
         if (currentShell == 2) {
             ID = "Shade";
         }
         if (currentShell == 3) {
             ID = "Kabutops";
         }
     }
 
     void OnGUI()
     {
         GUI.skin = skin;
         GUI.Label (new Rect(Screen.width - 350, 40, 100, 200), "Bank:" + (PlayerPrefs.GetInt("Coins Gathered")), skin.GetStyle ("Coin Counter"));
         GUI.Label (new Rect(Screen.width - 350, 5, 100, 200), "Prototype: Standard " + ID + " v" + (PlayerPrefs.GetInt("Death Number:")), skin.GetStyle ("Death Counter"));
         GUI.Label (new Rect(Screen.width /2-100, 40, 100, 200), "Press Enter To Accept", skin.GetStyle ("Press Enter"));
     }
 }
 

this is the player instantiating code:

 public GameObject[] shells;
 
 //shell instantiating
         print (PlayerPrefs.GetInt("Choosen Shell"));
         GameObject selectedshell = Instantiate(shells [(PlayerPrefs.GetInt ("Choosen Shell"))], new Vector3 (0,2,0), Quaternion.identity) as GameObject;
         selectedshell.transform.parent = transform;
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Toon_Werawat · Apr 15, 2016 at 07:50 AM

Here is suggestion. Let's use Auto-Property & Array in your code.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class Showroom_Manager : MonoBehaviour {
     public GUISkin skin;
     public string ID;
     
     public Vector3 distanceAmount;
     //public bool keyDown = false;
     
     //shells
     //public int shellQuantity = 2; this will no longer need because you can use ids.Length - 1

     //This is auto property.
     private int shl = 0; //This is a part of auto-property
     public int currentShell
     {
         get { return shl; }
         set
         {
             if (value < 0) { value = ids.Length - 1; }
             if (value > ids.Length - 1) { value = 0; }
             if (shl != value)
             {
                 transform.position += -distanceAmount;
                 ID = ids[value];
                 shl = value;
             }
         }
     }
     
     //New variable this variable can also modify in unty inspector
     public string[] ids = new string[]
     {
         "No-Shell",
         "Spark",
         "Shade",
         "Kabutops"
     };
     
     void Start()
     {
         PlayerPrefs.SetInt ("Choosen Shell", 0);
     }
     
     void Update ()
     {
         //right arrow command
         if (Input.GetKeyDown (KeyCode.RightArrow))
         {
             currentShell += 1;
         }
         
         if (Input.GetKeyDown (Keycode.LeftArrow)
         {
             currentShell -= 1;
         }
         
         //enter command
         if (Input.GetKeyDown (KeyCode.Return))
         {
             PlayerPrefs.SetInt ("Choosen Shell", currentShell);
                                            PlayerPrefs.Save();
             SceneManager.LoadScene("main menu");
         }
     }
 }
 
 void OnGUI()
 {
     GUI.skin = skin;
     GUI.Label (new Rect(Screen.width - 350, 40, 100, 200), "Bank:" + (PlayerPrefs.GetInt("Coins Gathered")), skin.GetStyle ("Coin Counter"));
     GUI.Label (new Rect(Screen.width - 350, 5, 100, 200), "Prototype: Standard " + ID + " v" + (PlayerPrefs.GetInt("Death Number:")), skin.GetStyle ("Death Counter"));
     GUI.Label (new Rect(Screen.width /2-100, 40, 100, 200), "Press Enter To Accept", skin.GetStyle ("Press Enter"));
 }
 }
 
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 LeoSaurusRex · Apr 16, 2016 at 02:27 AM 0
Share

Awwww man, thank you so much! you literally saved the future of my game (and my sanity). thanks a lot!!!! wokrs like a gem and saved me loads of lines of code on my different menus, etc.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can´t instantiate objects in list correctly 1 Answer

Changes to prefab instance generated from script aren't remembered 1 Answer

How to pass array variables from one script in one scene to another script in another scene? 2 Answers

Prefabs instantiated from an array are keeping their public int value 1 Answer

An Instantiated object to array problem 2 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