Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by yusufalp727 · Aug 26, 2021 at 12:48 PM · charactersaveshopbuymarket

How can I use 'buy character button'?

I made a character selection system and ı added 'buy button' for characters. I want that the player can select a character If player buys that character but he cannot continue the game without buying that character. How can I make this buying process? I added 'buy button' for some characters. When the player presses this button, the button is deleted and his money decreases. But I don't want the player to choose that character without pressing the buy button.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class Characters : MonoBehaviour
 {
     public GameObject[] characters;
     public int selectedCharacter;
 
     public int gold=450;
     public Text goldText;
 
     public GameObject[] buyButtons;
     public bool bought;
 
 
      void Start() 
      {
         goldText.text="Gold:" + gold.ToString();  
 
         selectedCharacter=PlayerPrefs.GetInt("selectedCharacter");
         characters=new GameObject[transform.childCount];
 
         for (int i = 0; i < characters.Length; i++)
         {
             characters[i]=transform.GetChild(i).gameObject;
         }
 
         foreach (GameObject go in characters)
         {
             go.SetActive(false);
         }
 
         if (characters[selectedCharacter])
         {
             characters[selectedCharacter].SetActive(true);
         }
 
         //First four characters are bought!
         if (selectedCharacter>4)
         {
             bought=false;
         }
         else
         {
             bought=true;
         }
     }
     public void PreviousCharacter()
     {
         characters[selectedCharacter].SetActive(false);
         selectedCharacter--;
         if (selectedCharacter < 0)
         {
             selectedCharacter=characters.Length-1;
         }
         characters[selectedCharacter].SetActive(true);
     }
     public void NextCharacter()
     {
         characters[selectedCharacter].SetActive(false);
         selectedCharacter++;
         if (selectedCharacter == characters.Length)
         {
             selectedCharacter=0;
         }
         characters[selectedCharacter].SetActive(true);
     }
 
     public void StartGame()
     {
         if (bought)
         {
             PlayerPrefs.SetInt("selectedCharacter", selectedCharacter);
         SceneManager.LoadScene(1,LoadSceneMode.Single);   
         }
         else
         {
             
             Debug.Log("cannot start");
         }
         // PlayerPrefs.SetInt("selectedCharacter", selectedCharacter);
         // SceneManager.LoadScene(1,LoadSceneMode.Single);
     }
 
     public void BuyButton()
     {
         if (gold >= 100)
         {
             buyButtons[0].SetActive(false);
             gold-=100;
             goldText.text="Gold:" + gold.ToString();
             bought=true;
         } 
     }
     public void BuyButton1()
     {
         if (gold >= 100)
         {
             buyButtons[1].SetActive(false);
             gold-=100;
             goldText.text="Gold:" + gold.ToString();
             bought=true;
         }
     }
     public void BuyButton2()
     {
         if (gold >= 100)
         {
             buyButtons[2].SetActive(false);
             gold-=100;
             goldText.text="Gold:" + gold.ToString();
             bought=true;
         }
     }
     public void BuyButton3()
     {
         if (gold >= 100)
         {
             buyButtons[3].SetActive(false);
             gold-=100;
             goldText.text="Gold:" + gold.ToString();
             bought=true;
         }
     }
     public void BuyButton4()
     {
         if (gold >= 100)
         {
             buyButtons[4].SetActive(false);
             gold-=100;
             goldText.text="Gold:" + gold.ToString();
             bought=true;
         }
     }
 }

alt text

asd.png (27.1 kB)
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

Answer by armandas2005 · Aug 26, 2021 at 05:10 PM

Hi, so i made this system in 3d. I used scriptableObjects for that. first you have to put the characters script on a empty object. If you done that you have to add characters in character list, to do that you have to rightclick in your assets folder and go to c(create > character). if you done that u have to declare a character object in there and the gold amount(how much it cost) (IMPORTANT: the object must have the dontdestroyonload script!!!). After u done that u have to add this character object to the characters list. If you done all that u have to make some buttons (Rightclick in scene > UI > Button). In the button there is a "component" called "On Click()". in the bottom right corner of this component there is a "+", you have to click on that. In the object " section" u have to add your empty Gameobject with the characters script on it. If you done that u have to click on Function and go to the character section. In there u can select functions of the script, like "next" or "buy". that should be it.

Add this script on a empty GameObject using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;

 public class Characters : MonoBehaviour
 {
     public List<Character> characters;
     public int selectedCharacter;
 
     public int charactersInList;
 
     public int gold = 450;
     public Text goldText;
 
     public GameObject previewObject;
 
     public bool canStart;
 
     void Start()
     {
         previewObject = Instantiate(characters[selectedCharacter].characterObject, transform.position, transform.rotation);
 
         goldText.text = "Gold:" + gold.ToString();
 
         selectedCharacter = PlayerPrefs.GetInt("selectedCharacter");
 
         Refresh();
     }
 
     public void Refresh()
     {
         charactersInList = 0;
         for (int i = 0; i < characters.Count; i++)
         {
             charactersInList++;
         }
         charactersInList--;
     }
 
     void Update()
     {  
         if (Input.GetAxis("Mouse ScrollWheel") > 0)
         {
             Next();
         }
         else if(Input.GetAxis("Mouse ScrollWheel") < 0)
         {
             Back();
         }
     }
 
     private void SelectSkin()
     {
         if(previewObject != null)
         {
             Destroy(previewObject);
             previewObject = Instantiate(characters[selectedCharacter].characterObject, transform.position, transform.rotation);
         }
         Debug.Log("m");
     }
 
     public void Next()
     {
         if (selectedCharacter < charactersInList)
         {
             selectedCharacter++;
             SelectSkin();
         }
     }
 
     public void Back()
     {
         if (selectedCharacter > 0)
         {
             selectedCharacter--;
             SelectSkin();
         }
     }
 
     public void StartGame()
     {
         if (canStart)
         {
             PlayerPrefs.SetInt("selectedCharacter", selectedCharacter);
             SceneManager.LoadScene(1, LoadSceneMode.Single);
         }
         else
         {
             Debug.Log("you have to pay First!");
         }
  
     }
 
     void Buy()
     {
         if(gold >= characters[selectedCharacter].Gold)
         {
             gold -= characters[selectedCharacter].Gold;
             canStart = true;
             StartGame();
         }
     }
 }

Dont do this script on any object, it just have to be in your asset folder(to create characters click (create > characters)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 [CreateAssetMenu]
 public class Character : ScriptableObject
 {
     public int Gold;
     public GameObject characterObject;
 }
 

To every character visual object u have to add this script, so the object dont disappear after loading into a new scene

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DontDestroy : MonoBehaviour
 {
 
     public void Awake()
     {
         DontDestroyOnLoad(this.gameObject); //Other stuff 
     }
 
 }

I hope i could help if you have any questions or u run into some issues with these scripts, tell me, i will give my best to help you :D

Comment
Add comment · Show 6 · 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 yusufalp727 · Aug 26, 2021 at 05:40 PM 0
Share

Thank you very much for answering. In my codes I have made some changes and now the player cannot continue the game without buying selected character. If selected character has buy button and if the player buys that character he can continue the game. First 4 characters are bought but when selected character is one of these 4 characters player cannot continue the game but he should!

avatar image armandas2005 yusufalp727 · Aug 26, 2021 at 05:49 PM 0
Share

can u send me the changes ur changes?

avatar image yusufalp727 armandas2005 · Aug 26, 2021 at 06:00 PM 0
Share

I edited my question

avatar image armandas2005 yusufalp727 · Aug 26, 2021 at 05:53 PM 0
Share

if u should have used the scripts i have send you than you have to do the buy function in Characters scripts public and add this to your button becouse the bool canStart gets activated only there

 public void Buy()
 {
 ...
avatar image yusufalp727 armandas2005 · Aug 27, 2021 at 11:54 AM 0
Share

Can you help me on my codes?

Show more comments

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

171 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Help with Shop Script C# 0 Answers

How to save an item as bought in player prefs? 0 Answers

I am trying to make a shop system but I am having this doubt 1 Answer

Experience with Turbo Squid or other? Mocap? 1 Answer

Can anyone help me figure out why PlayerPrefs wont save the money value?,Can anyone help me as to why my money won't save in my game? Using PlayerPrefs 1 Answer


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