So my ultimate goal is that when my script picks a number ( That doesn’t surpass the amount of game objects I’m concentrated on ) I get the gameobject that correlates to the number and identify its color, then to apply it to another. I’m having difficulty on the first step though, I can’t figure out a way to get the game object and my searches haven’t been assisting. My original code is
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, CirclePart.Length);
}
I thought this would work but I get the error can’t convert gameobject to int
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorArray : MonoBehaviour
{
public GameObject[] CirclePart;
public int activeCircle;
public GameObject[] Arrow;
int 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];
}
}
If any one has an idea of what I should be doing, your help is appreciated
My active circle is the random number picker
Should I be doing that to the ColorGrabber or the activeCircle? I get the error
Assets\ColorArray.cs(29,24): error CS0029: Cannot implicitly convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.GameObject[ ]’
When I assign the ColorGrabber as a GameObject
Did some further playing around and did this
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 = activeCircle[CirclePart];
}
}
But get the error
Assets\ColorArray.cs(29,24): error CS0021: Cannot apply indexing with [ ] to an expression of type ‘int’
If I switch the activeCircle to a GameObject I get more errors
You might want to review basic C# syntax, especially that related to arrays and variables.
This:
public GameObject[] ColorGrabber;
is an array of GameObjects. Could be zero, one, two, etc.
This:
public GameObject ColorGrabber;
is a single GameObject reference.
If you are retrieving a particular element from an array, you use the square bracket syntax and the expression inside those square brackets needs to be an integer, and it needs to be within the bounds of the size of the array.