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 Sskethan · Nov 22, 2016 at 03:05 PM · listarray of gameobjectsdice

Drop item based on die roll

How could I setup an array that would take the value of the die roll and drop an item that was setup for that value? ie: roll 1 - sword, roll 2 - axe roll 3 - knife ect... What I'm trying to do is at the end of each level I have the boss setup to bring the die screen up when the boss is killed, then the player clicks the die and whatever the die lands on the player will get that item. Here is what I have for the die script. Right now the player kills the boss and the screen pops up and I can click and roll the die.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class DiceRoll : MonoBehaviour
 {
     public Sprite[] Dice;
     private bool isRolling;
     private float totalTime;
     private float intervalTime;
     private int currrentDie;
     private bool dieRolled;
     private int currentTotal;
 
     Image die;
 
     void Start()
     {
         die = GameObject.Find("DieImage").GetComponent<Image>();
         Init();
     }
 
     private void Init()
     {
         totalTime = 0.0f;
         intervalTime = 0.0f;
         currrentDie = 0;
         dieRolled = false;
         die.sprite = Dice[currrentDie];
     }
 
     void Update()
     {
         if (isRolling)
         {
             intervalTime += Time.deltaTime;
             totalTime += Time.deltaTime;
 
             if (intervalTime >= 0.1f)
             {
                 //change die & rotation
                 currrentDie = Random.Range(0, 6);
                 die.transform.Rotate(0, 0, Random.Range(0, 360));
 
                 //set image to selected die
                 die.sprite = Dice[currrentDie];
                 intervalTime -= 0.1f;
             }
 
             if (totalTime >= 2.00f)
             {
                 isRolling = false;
                 dieRolled = true;
             }
         }
     }
 
     public void DieImage_Click()
     {
         if (!dieRolled)
         {
             isRolling = true;
         }
 
         if (!isRolling)
         {
             Init();
             isRolling = true;
         }
     }
 
     public void PanelTestButton_Click()
     {
         GameObject panel = GameObject.Find("DiePanel");
         Animator animator = panel.GetComponent<Animator>();
         animator.Play("DiePanelOpen");
     }
 
     public void PanelOKButton_Click()
     {
         GameObject panel = GameObject.Find("DiePanel");
         Animator animator = panel.GetComponent<Animator>();
         animator.Play("DiePanelClose");
     }
 }

Comment
Add comment · Show 2
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 JatMidGrade · Nov 22, 2016 at 03:49 PM 0
Share

Are you wanting to drop the item within the Game World, or place said Item directly into an inventory? I'm assu$$anonymous$$g the Items are already set up, so there would be an if statement for each currentDie integer value placing that item either in the game world or the inventory itself depending on what you are wanting.

avatar image Sskethan JatMidGrade · Nov 22, 2016 at 06:24 PM 0
Share

Yes there is an inventory already in place. The item would be placed into the inventory depending on what your roll is. I would like to be able to set the 6 items manually in a array or something and add it to inventory. I know how to add items to the inventory and how to have items drop. $$anonymous$$y problem is I'm not sure how to set up the die to know what item to add. How do i match the value of the die with the item in the array?

1 Reply

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

Answer by oStaiko · Nov 22, 2016 at 04:38 PM

What you want is a switch function, but then again I may just be saying that as I am a switch enthusiast.

 Weapon[] Swords;
 Weapon[] Bows;
 Weapon[] Armor;
 
 void GetItem (int result)
 {
     switch (result)
     {
         case 0: return Swords[Random.Range(0, Swords.Length);
         case 1: return Bows[Random.Range(0, Bows.Length);
         case 0: return Armor[Random.Range(0, Armor.Length);
     }
 }

I mean, you could ALSO do an array of arrays... but that's no fun...

 Weapon[][] weaponList = new Weapon[][] 
 {
     new Weapon[] {sword1, sword2, sword3},
     new Weapon[] {bow1, bow2, bow3, bow4},
     new Weapon[] {armor1, armor2, amor3}
 };
 
 Weapon GetItem (result)
 {
     return weaponList[result][Random.Range(0,weaponList[result].Length);
 }
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 Sskethan · Nov 22, 2016 at 07:03 PM 0
Share

Thank you, I was able to use a switch to get it working.

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

79 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

Related Questions

HOW TO.. Grouping gameobject and move object in arraylist (Runtime) 0 Answers

how to assign name to gameobject list from string list ? 0 Answers

3 Dice checking trouble......Array? List? Linq? 0 Answers

LoadScene not working 1 Answer

Hide or show objects in a list not working correctly 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