- Home /
Create A Counter And GameObjects Unique ID
Hello, this is what my game is like, you’re a cube who when touches another game object (i.e., the floor, made from multiply squares with separate box colliders) then their change colour:
So this is what l need help with, l want some sort of counter which is displayed on screen to plus one each time a floor game object (only the floor ones) are hit. What l need help with is some way of setting up a unique ID to each floor game object so it doesn’t count the same floor game object more than once. I want to make it so when all the floor is covered as shown below then an animation plays:
This is my code for the cube colliding with the floor game objects to make them change texture:
using UnityEngine;
using System.Collections;
public class CubeCollision : MonoBehaviour {
public Texture ColourRed;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision cul)
{
if (cul.gameObject.name == "PlayerCube")
GetComponent<Renderer>().material.mainTexture = ColourRed;
}
}
Any help would be greatly appreciated.
You can generate unique IDs using GUID:
string id = System.Guid.NewGuid().ToString();
Answer by NeverHopeless · Aug 19, 2015 at 09:01 AM
To create a unique ID, you can create a static variable (say FloorNumber) in a script and on Start
increase this variable and store in a class variable. Attach this script to the game object you want to have a uniqueID and use class variable as a floor number.
class Floor {
static int FloorNumber;
int myFloorNumber;
void Start()
{
FloorNumber ++;
myFloorNumber = FloorNumber;
}
void PrintMyFloorNumber()
{
Debug.Log("MyFloorNumber is: " + myFloorNumber); // Here is the unique ID
}
}