hey guys, I’m pretty new to unity and C#, and i’m really struggling with wrapping my mind around how GameObjects work, and how they interact with their script components, and how MonoBehavior classes work also.
For starters, I want to make a grid full of marbles that can be moved around by touch or keyboard input, whatever the case may be. So I have a simple Marble class that moves toward its destination each frame.
public enum mType { red, orange, yellow, green, blue, purple, stone, star }
public class Marble : MonoBehaviour {
public GameObject parent;
public Vector3 destination;
[SerializeField]
float speed = 10f;
public int index;
// Use this for initialization
void Start () {
//TODO: figure out how to pass start data from MarbleGrid to Marble
}
// Update is called once per frame
void Update () {
// move marble towards destination
if (this.destination != this.transform.position)
{
float step = speed * Time.deltaTime;
this.transform.position = Vector3.MoveTowards(this.transform.position, destination, step);
}
else
{
// TODO: find some way to update MarbleGrid's array so that it knows we're at the destination now
}
}
After that, I have a MarbleGrid class, also relatively simple.
public class MarbleGrid : MonoBehaviour {
List<GameObject> marbles;
public GameObject marblePrefab;
int[,] indexes;
int rows = 11;
int columns = 5;
int score = 0;
int marbleCount = 0;
float cdAddMarbles = 2f;
float cdLeft = 2f;
void Start (){
indexes = new int[columns, rows];
marbles = new List<GameObject>();
GameObject go = (GameObject)Instantiate(marblePrefab, new Vector3(0, 0, 0), Quaternion.identity);
go.transform.SetParent(this.transform);
marbles.Add(go);
}
// this is the part that blows my mind. I get a red underline under q.index.Equals(i) saying
// "GameObject does not carry a definition for 'index'" or something like that. Why doesn't
// the Gameobject marble that I created have any of my Marble class data? what am I
// doing wrong here, or lacking fundamental understanding of?
public GameObject PickMarbleFromList(int x, int y)
{
int i = indexes[x, y];
GameObject go = marbles.Find(q => q.index.Equals(i));
return go;
}
{
Thanks in advance guys!