I’ve been looking online to figure out how to do this but haven’t found much yet, so if anyone could help that’d be great. I’ve got an array that holds 8 GameObjects listed through elements 0-7. I’ve got another component that randomly picks a number between 0 and 7. How can I retrieve the color of the element with the same number as the one resulted from the random pick.
Here’s my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorArray : MonoBehaviour
{
public GameObject[] CirclePart;
public int activeCircle;
public GameObject[] Arrow;
// Start is called before the first frame update
void Start()
{
NewCircle();
}
// Update is called once per frame
void Update()
{
}
void NewCircle()
{
activeCircle = Random.Range(0, 8);
}
}
If anyone could help, once again, I’d appreciate it
Arrays have indexes in the square brackets that can go from 0 to the size-1 (size 8 is 0 to 7, which is 8 numbers). CirclePart[0] or CirclePart[7] are obvious. But variables can also be indexes. An easy one: int x=1; CirclePart[×] is the same as CirclePart[1].
Fun fact: the way random picks 1 less than the last, and arrays are numbered up to size-1 go together. That’s not completely an accident. activeCircle=Random.Range(0, CirclePart.Length) works perfectly. The -1’s cancel out.
You’re using a numeric literal instead of a variable. Your code breaks if you change the length of the array in the inspector and forget to edit the script. While using a variable, in this case CirclePart.Length, works just fine with any length array greater than 0, so doesn’t require you to edit the script just because you changed the array in the inspector. Much better design for a trivial amount of additional code.
It’s a teaching trick. CirclePart[1] and int x=1; CirclePart[×] are the same thing in the end. But using x preset to 1 shows that an int variable can be inside of the [ ]'s, and how that works – look up the value of x first. When we come to CirclePart[activeCircle], that’s just another variable in [ ]'s. activeCircle wasn’t preset to a single number, but it’s still the same idea – if activeCircle is 5, it’s CirclePart[5].
Okay that makes sense to me now. I’m assuming that because the variable is assigned to the number, I’m able to get the color from the result of the activeCircle a lot smoother. I’m pretty new to coding so I’m trying to figure out how to implement that into the code. I’m not entirely sure I’m on the right track, but I think I’ve got a general idea. I tried doing it by
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorArray : MonoBehaviour
{
public GameObject[] CirclePart;
public int activeCircle;
int x = 1;
public GameObject[] Arrow;
// Start is called before the first frame update
void Start()
{
NewCircle();
CirclePart[x];
}
// Update is called once per frame
void Update()
{
}
void NewCircle()
{
activeCircle = Random.Range(0, CirclePart.Length);
}
}
So I did that but I get this error
Assets\ColorArray.cs(16,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Am I doing that correct? I’ve been trying multiple other ways off the top but they all get numerous errors. I’m not sure if I’m just missing something slight or if it’s wrong completely. Also I know that I’ve just listed one variable with that method but once I get this working, I’ll apply it to all of them. I wasn’t going to respond yet just because I want to try to do as much of this myself and learn from it, but I’ve been brain dead to this point trying it out. Will get back at it after some rest.
The thing with x is an example. It wouldn’t be in real code. I think you’re getting tangled up in all the various parts.
Suppose you had GameObject g;. How would you gets its color? I’d mess around with that and see how it works for a bit. Next, if you set g=CirclePart[0];, then getting the color of g would really be getting the color of the first circle part.
If g was set to circlePart[activeCircle] beforehand, … now the same boring old line finding g’s color is actually finding the color of that part.
Hey so I think I’m pretty close to completing this goal. My code is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorArray : MonoBehaviour
{
public GameObject[] CirclePart;
public int activeCircle;
public GameObject Arrow;
public GameObject ColorGrabber;
// Start is called before the first frame update
void Start()
{
NewCircle();
}
// Update is called once per frame
void Update()
{
}
void NewCircle()
{
activeCircle = Random.Range(0, CirclePart.Length);
ColorGrabber = CirclePart[activeCircle];
GetComponent<Renderer>().material.color = ColorGrabber.GetComponent<Renderer>().material.GetColor("_Color");
Arrow.GetComponent<Renderer>().material.SetColor(ColorGrabber);
}
}
I’ve been trying to search through forums and unity’s website but haven’t been able to find anything helpful, specifically in getting the color of the Arrow the same as the one in ColorGrabber. I get the error
Assets\ColorArray.cs(31,49): error CS1501: No overload for method ‘SetColor’ takes 1 arguments
Thoughts? The color of the arrow just has to be the same as the ColorGrabber, as that gets the GameObject that matches the number drawn.