• 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 Nightslash1984 · Nov 27, 2021 at 06:17 AM · arraylistspritesindex

How do I return the index of an array of sprites as an int?

I am making a Yahtzee game. I have 3 files one has the rolling of the dice function and a list of sprites representing the dice face and a list of buttons representing each individual dice. : using UnityEngine; using UnityEngine.UI;

 public class RollRandom : MonoBehaviour
 {
     //dice for dice roller
     public int dice1;
     public int dice2;
     public int dice3;
     public int dice4;
     public int dice5;
     //faces
     public Sprite[] diceFaces;
     //GameObjects
     public Button[] diceSprites;
 
     void Start()
     {
         dice1 = Random.Range(1, 7);
         dice2 = Random.Range(1, 7);
         dice3 = Random.Range(1, 7);
         dice4 = Random.Range(1, 7);
         dice5 = Random.Range(1, 7);
 
         GameObject SelectionManager = GameObject.Find("SelectionManager");
         SelectDice SelectDice = SelectionManager.GetComponent<SelectDice>();
 
         if (SelectDice.selected1 == false)
         {
             diceSprites[0].image.sprite = diceFaces[dice1 - 1];
         }
         if (SelectDice.selected2 == false)
         {
             diceSprites[1].image.sprite = diceFaces[dice2 - 1];
         }
         if (SelectDice.selected3 == false)
         {
             diceSprites[2].image.sprite = diceFaces[dice3 - 1];
         }
         if (SelectDice.selected4 == false)
         {
             diceSprites[3].image.sprite = diceFaces[dice4 - 1];
         }
         if (SelectDice.selected5 == false)
         {
             diceSprites[4].image.sprite = diceFaces[dice5 - 1];
         }
 
     Debug.Log("Started");
     }
 
     public void GenerateNumber() {
         dice1 = Random.Range(1, 7);
         dice2 = Random.Range(1, 7);
         dice3 = Random.Range(1, 7);
         dice4 = Random.Range(1, 7);
         dice5 = Random.Range(1, 7);
         int z = 0;
 
         GameObject SelectionManager = GameObject.Find("SelectionManager");
         SelectDice SelectDice = SelectionManager.GetComponent<SelectDice>();
 
         if (SelectDice.selected1 == false)
         {
             diceSprites[0].image.sprite = diceFaces[dice1 - 1];
         }
         if (SelectDice.selected2 == false)
         {
             diceSprites[1].image.sprite = diceFaces[dice2 - 1];
         }
         if (SelectDice.selected3 == false)
         {
             diceSprites[2].image.sprite = diceFaces[dice3 - 1];
         }
         if (SelectDice.selected4 == false)
         {
             diceSprites[3].image.sprite = diceFaces[dice4 - 1];
         }
         if (SelectDice.selected5 == false)
         {
             diceSprites[4].image.sprite = diceFaces[dice5 - 1];
         }
 
         int index = System.Array.FindIndex(diceFaces, 0, 5, );
     }
 }

and another file to select dice t hold and not roll with selecting the dice but that one is irrelevant in t$$anonymous$$s case. but the t$$anonymous$$rd one is to manage the playing of the dice in certain places for points. using UnityEngine;

 public class PlayManager : MonoBehaviour
 {
     public int[] faceValue;
     public UnityEngine.UI.Button dice1;
 
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
     public void Manager()
     {
         GameObject RNGManager = GameObject.Find("RNGManager");
         RollRandom rollRandom = RNGManager.GetComponent<RollRandom>();
 
         int index = rollRandom.diceFaces.FindIndex();
     }
 }

what I need to do is return the index of that diceFaces list for each dice so I can add 1 to it and get the value of it. what do I put in the parenthesis after int index = rollRandom.diceFaces.FindIndex();

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 sacredgeometry · Nov 27, 2021 at 12:23 PM 0
Share
avatar image sacredgeometry · Nov 27, 2021 at 01:57 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by sacredgeometry · Nov 27, 2021 at 03:00 PM

Here is a video explaining an alternative implementation:

https://www.youtube.com/watch?v=Et_GO_naSKk


And heres the code:

In t$$anonymous$$s implementation to get the values back you can just access the values in your foreach loop or if its not going to cause performance problems to create a new list do somet$$anonymous$$ng like:

var values = Dice.Select(die => die.Roll()) for all new values or var values = Dice.Select(die => die.Value) for all existing.


 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class GameManager : MonoBehaviour
 { 
     private List<Die> dice { get; set; }
 
     private void Start() 
     {
         dice = FindObjectsOfType<Die>().ToList();
     }
 
     private void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             dice.ForEach(die => die.Roll());
         }
     }
 }



 using UnityEngine;
 
 public class Die : MonoBehaviour
 {
     public Sprite[] Sprites;
 
     public int Value;
 
     private int sides => Sprites.Length;
 
     private SpriteRenderer spriteRenderer { get; set; }
     
 
     private void Start()
     {
         spriteRenderer = GetComponent<SpriteRenderer>();
     }
 
     public int Roll()
     {
         Value = Random.Range(1, sides);
         spriteRenderer.sprite = Sprites[Value - 1];
         return Value;
     }
 }

Comment
Add comment · Show 4 · 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 sacredgeometry · Nov 27, 2021 at 03:09 PM 0
Share
avatar image Nightslash1984 · Nov 27, 2021 at 05:11 PM 1
Share
avatar image sacredgeometry Nightslash1984 · Nov 27, 2021 at 05:21 PM 0
Share
avatar image Nightslash1984 sacredgeometry · Nov 27, 2021 at 05:26 PM 1
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

150 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

Related Questions

List gives "Array index is out of range" for no reason 2 Answers

How do I find a local variables index in a foreach loop? 1 Answer

For all elements above a certain index 1 Answer

How do I check a List[0], when it's nothing? 1 Answer

Find specific element when duplicates exist in list. 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