- Home /
How to change the variables from an specific clone script?
I'm trying to make a cell game, in it the cells will gather food which will increase their fullness and once they reach a certain level of fullness they replicate, the thing is that when the cells take the food, the fullness increases only in the last cell instantiated instead of in the cell that touched the food
Script in the food:
void OnTriggerEnter2D(Collider2D collisor)
{
if (collisor.tag == "Cell")
{
cellFoodLogic.instace.fullness += 10;
Debug.Log("Fullness: " + cellFoodLogic.instace.fullness);
Destroy(this.gameObject);
}
}
script on cells:
public class cellFoodLogic : MonoBehaviour
{
public static cellFoodLogic instace;
public GameObject daughterCell1;
public GameObject daughterCell2;
public int fullness;
public float fullnessDecreaseSpeed;
private float fullnessDecreaseTimer;
Transform tr;
// Start is called before the first frame update
void Start()
{
instace = this;
tr = GetComponent<Transform>();
fullness = 50;
fullnessDecreaseTimer = fullnessDecreaseSpeed;
}
// Update is called once per frame
void Update()
{
fullnessDecreaseTimer -= Time.deltaTime;
if(fullnessDecreaseTimer<=0)
{
fullness -= 10;
fullnessDecreaseTimer = fullnessDecreaseSpeed;
Debug.Log("Fullness: "+fullness);
}
if (fullness<=0)
{
Destroy(this.gameObject);
}
if(fullness>=100)
{
fullness = 50;
Instantiate(daughterCell1, tr.position, Quaternion.identity);
}
}
}
Answer by unity_ek98vnTRplGj8Q · Jan 03, 2020 at 04:47 PM
The issue is that you are using a STATIC variable to keep track of your cells. public static cellFoodLogic instace;
This is essentially what is known as a singleton: it is useful if you only expect 1 instance of a class to ever exist. You, however, will have many cell instances, and you want the collision trigger to find the associated cell instance. This is not possible using the static variable in the way that you are. Every time you call cellFoodLogic.instance
it will always point to the same cell, no matter where you call it from.
To fix this, you will need to find a way to get the specific instance associated with the cell. The easiest way to do this is to use GetComponent
from the collision script.
Try this:
void OnTriggerEnter2D(Collider2D collisor)
{
if (collisor.tag == "Cell")
{
//cellFoodLogic.instace.fullness += 10; <------Faulty line of code
cellFoodLogic cell = collisor.gameObject.GetComponent<cellFoodLogic>();
if(cell!=null){
cell.fullness += 10;
Debug.Log("Fullness: " + cell.fullness);
}
Destroy(this.gameObject);
}
}
I have similar problem do you think you could try to help?
I am trying to make a game where multiple enemies come down to attack. I Instantiate them and have them randomly come down. But when I check if they are touching a bullet, only the last instantiated enemy can check it. Here is the link to original post with the code: https://answers.unity.com/questions/1888391/how-do-i-check-the-variable-of-a-specific-clone.html?sort=votes
Your answer

Follow this Question
Related Questions
Tetris, Tetrominoes Clones Spawn Same Time 0 Answers
Checking if object intersects? 1 Answer
How do you only Instantiate only once when pressing a button once? 2 Answers
About cloned scripts 1 Answer
How to compare the positions of two or more instantiated gameobjects (clones) with different tags? 1 Answer