Question by
Xenogrease · Jan 21, 2021 at 10:10 AM ·
c#scripting problemunityeditor
how to manage different objects with the same script.
Im making a game where a lot of components are repeated. A betting board for example where the tiles need to behave the same. One example is when a tile is clicked, a corresponding betting chip should appear. Therefore, I copied the tile object, however it doesn't work. I'm assuming this is because it uses the same script and the script already has inputs from the original object and can't take new ones from the copied object. Maybe I'm wrong that's just my thesis. But can anyone help with how to amend this? As opposed to creating new blank scripts and copying all the code 10x which seems highly inefficient.
this is some of the relevant code for reference.
public class ImageClick : MonoBehaviour {
//Image fields created for each chip on the tile
public Image redchip;
[SerializeField] //Image fields appear in inspector so chip images can be drag and dropped
public Image bluechip;
[SerializeField]
public Image greenchip;
[SerializeField]
//the public elements of the ChipChooser script are accessible in this script.
private ChipChooser myChipChooser;
//at initialisation the images visibility will be off
public void Start () {
redchip.enabled = false;
bluechip.enabled = false;
greenchip.enabled = false;
}
//when mouse click input is detected, the images visibility booelan will be reversed
public void OnMouseDown() {
if (myChipChooser.redclicked == true)
{
redchip.enabled = true; //visibility always true when clicked
ChipCounter.redChipNumber += 1;
}
if (myChipChooser.blueclicked == true)
{
bluechip.enabled = true;
ChipCounter.blueChipNumber += 1;
}
if (myChipChooser.greenclicked == true)
{
greenchip.enabled = true;
ChipCounter.greenChipNumber += 1;
}
}
}
Comment