- Home /
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();
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.
Ive recorded you a video explaining the refactoring let me upload it
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;
}
}
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.
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.
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.