Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 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 this case. but the third 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

I am not sure I understand why you need it. The index is an address/ key you use to look it up. The thing that needs it already should have it as its the one looking it up. Can I suggest that maybe the way you have written this is a bit convoluted?

What are you trying to build? Maybe we can solve the problem as a side effect of refactoring it.

avatar image sacredgeometry · Nov 27, 2021 at 01:57 PM 0
Share

Ive recorded you a video explaining the refactoring let me upload it

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 this 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 something 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

It may take a little time for the video quality to improve as its still processing and I cleaned up the casing in the examples above. Alternatively you may also what to move the sprite changing logic into the Die.Value like:


 private int _value;
 public int Value 
 {
     get => _value;
     set
     {
         _value = value;
         _spriteRenderer.sprite = Sprites[value - 1];
     }
 }
 



as then anytime you change the value it will automatically update the sprite and obviously it needs error handling etc.

avatar image Nightslash1984 · Nov 27, 2021 at 05:11 PM 1
Share

So just realized I'm kinda dumb because I can just use the int the random number generator returns I don't even need the index value.

avatar image sacredgeometry Nightslash1984 · Nov 27, 2021 at 05:21 PM 0
Share

Yep, as I was trying to elude to. Your code seemed a bit confused which is why I tried to demonstrate how I would approach it. Hopefully it illu$$anonymous$$ated a few of the issues with it.

avatar image Nightslash1984 sacredgeometry · Nov 27, 2021 at 05:26 PM 1
Share

yes thank you so much

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

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

A node in a childnode? 1 Answer

For all elements above a certain index 1 Answer

How do I check a List[0], when it's nothing? 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