I’m facing an issue in my Unity project where different models, created using Instantiate and with scripts added using AddComponent, are referencing the same variables from a single script instead of having their own individual variables. This is causing problems because important variables like afterRotate and isRotating are being accessed by multiple models simultaneously.
Here’s a brief overview of what I’m doing:
I’m instantiating multiple models.
I’m adding a script to each model using AddComponent.
Despite this, all models seem to reference the same instance of the script’s variables.
This is severely impacting my project as the state of one model affects the others. For instance, when one model sets isRotating to true, it impacts all other models.
Can anyone help me understand why this might be happening? How can I ensure that each model has its own instance of the script variables?
Thank you in advance for your help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Entity_Spawner : MonoBehaviour
{
public Map map;
public Human_Group human_Group;
public GameObject Human1_model;
public Transform spawn_Point;
void Start()
{
}
void Update()
{
}
public void entity_Spawn()
{
GameObject newUnit = Instantiate(Human1_model, spawn_Point.position, Quaternion.identity);
Human_Movement human_Movement = newUnit.AddComponent<Human_Movement>();
human_Movement.set_Object_Map(map);
human_Movement.set_Human_Group(human_Group);
newUnit.SetActive(true);
}
}
I tried to find the reason, but I couldn’t find it. I don’t use static, chatgpt says the problem is public, is he right? If you need more code I can provide it
user25479256 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.