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 /
  • Help Room /
avatar image
0
Question by Tetzio · Nov 27, 2018 at 09:32 PM · prefabvalues

Store separate values from a script on each identical prefab

Hello, currently I am trying to create a set of scripts that will allow the player to walk up to a prefab, interact with it, and walk away. These identical prefabs will be spread throughout the level. The issue I am having is that when the player walks up to a prefab, the information is saved across all instances of the prefab and I need each prefab to store separate values. Currently, this script is on the player, when they walk up to a instance of the prefab it will open up a menu and options and values will be stored from it. I'm not sure how to make each prefab store separate values, or if it's possible. I'm sure there is a better way to do what I'm currently doing but I'm somewhat new to Unity and C#.

 public class BattleUIManager : MonoBehaviour
 {
     public Transform trick1;
     public Transform trick2;
     public Button option1B;
     public Button option2B;
     public Slider energySlider;
     public Slider happySlider;
     public bool trick1Once;
     public bool trick2Once;
     bool doingTrick = false;
     public bool activeVillager = false;
     public int Energy;
     public int Happiness;
 
     void Start()
     {
         trick1Once = true;
         trick2Once = true;
         Button opt1 = option1B.GetComponent<Button>();
         Button opt2 = option2B.GetComponent<Button>();
         opt1.onClick.AddListener(Option1);
         opt2.onClick.AddListener(Option2);
         energySlider.value = 1;
     }
     void Option1()
     {
         int energyCost = 10;
         if (Energy - energyCost >= 0 && trick1Once)
         {
             doingTrick = true;
             Energy -= energyCost;
             Instantiate(trick1);
             StartCoroutine(ExampleTrick());
             Debug.Log("Calling Trick 1");
             trick1Once = false;
         }
     }
     void Option2()
     {
         int energyCost = 20;
         if (Energy - energyCost >= 0 && trick2Once)
         {
             doingTrick = true;
             Energy -= energyCost;
             Instantiate(trick2);
             Debug.Log("Calling Trick 2");
             StartCoroutine(ExampleTrick());
             trick2Once = false;
         }
     }
     private void Update()
     {
         if (!doingTrick && activeVillager)
         {
             option1B.gameObject.SetActive(true);
             option2B.gameObject.SetActive(true);
         }
         else
         {
             option1B.gameObject.SetActive(false);
             option2B.gameObject.SetActive(false);
         }
 
         happySlider.value = Happiness;
     }
     private void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Villager"))
         {
             activeVillager = true;
         }
     }
     private void OnTriggerExit(Collider other)
     {
         if (other.gameObject.CompareTag("Villager"))
         {
             activeVillager = false;
         }
     }
     IEnumerator ExampleTrick()
     {
         energySlider.value = Energy * 0.01f;
         
         yield return new WaitForSeconds(10);
         doingTrick = false;
     }
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

183 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 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

References of a Prefab 1 Answer

Can't access a value on a component of a script I made for a prefab. 0 Answers

Prefab instances reverting to their default values upon play. 0 Answers

(VR) Switch Player Prefabs on Load Scene 1 Answer

Mesh missing in prefab 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