I’ve been making a 3d unity game recently where two types prefabs will spawn in a building at random times. You can compliment them so that they can be happy or insult them to make them sad. This all works fine, except that once the second prefab spawns, that prefab doesn’t work. Its only the first spawned prefab that does, and I’m wondering what is wrong. I’m fairly new to unity, so I’m probably going about this the wrong way. Help would really be appreciated, the scripts related to the issue are below. (I don’t get any error messages).
Spawner Script:
public GameObject[] enemies;
public Vector3 spawnValues;
public float spawnWait;
public float spawnMostWait;
public float spawnLeastWait;
public GameObject radio;
public GameObject studentText;
public int startWait;
public bool stop;
int randEnemy;
// Use this for initialization
void Start () {
StartCoroutine(waitSpawner());
}
// Update is called once per frame
void Update () {
spawnWait = Random.Range(spawnLeastWait, spawnMostWait);
}
IEnumerator waitSpawner()
{
yield return new WaitForSeconds(startWait);
while (!stop)
{
randEnemy = Random.Range(0, 2);
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 0, Random.Range(-spawnValues.z, spawnValues.z));
Instantiate(enemies[randEnemy], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
studentText.SetActive(true);
yield return new WaitForSeconds(2);
studentText.SetActive(false);
yield return new WaitForSeconds(spawnWait);
}
}
}
Female Prefab Script that doesn’t work after second spawn:
public GameObject gameManager;
public int happiness = 0;
public int sadness = 0;
public int musicHappiness = 0;
public Material happyMaterial;
public Material neutralMaterial;
public Material sadMaterial;
// Use this for initialization
void Start()
{
gameManager = GameObject.FindGameObjectWithTag("GameManager");
print("script working");
}
// Update is called once per frame
void Update()
{
if (gameManager.GetComponent<ManageScript>().complimentGirl == true)
{
happiness = happiness + 1;
sadness = sadness - 1;
gameManager.GetComponent<ManageScript>().complimentGirl = false;
}
if (gameManager.GetComponent<ManageScript>().insultGirl == true)
{
sadness = sadness + 1;
happiness = happiness - 1;
gameManager.GetComponent<ManageScript>().insultGirl = false;
}
if (gameManager.GetComponent<ManageScript>().leaveGirl == true)
{
print("Left Girl");
gameManager.GetComponent<ManageScript>().leaveGirl= false;
}
if (happiness >= sadness)
{
GetComponent<Renderer>().material = happyMaterial;
}
if (happiness <= sadness)
{
GetComponent<Renderer>().material = sadMaterial;
}
if (happiness == sadness)
{
GetComponent<Renderer>().material = neutralMaterial;
}
if (gameManager.GetComponent<ManageScript>().musicIsOn == true)
{
musicHappiness = 1;
happiness = happiness + musicHappiness;
gameManager.GetComponent<ManageScript>().musicIsOn = false;
}
if (gameManager.GetComponent<ManageScript>().musicIsOff == true)
{
musicHappiness = -1;
happiness = happiness + musicHappiness;
gameManager.GetComponent<ManageScript>().musicIsOff = false;
}
}
void OnMouseDown() {
print("Talking is working");
gameManager.GetComponent<ManageScript>().freezeMove();
gameManager.GetComponent<ManageScript>().girlTalk();
}
}
Again, this is kind of weird because the other scripts in these prefabs work no matter what, I’ve tested a print on start on these scripts to see if they run at all and they don’t seem to.
Male Prefab Script that doesn’t work after second spawn:
public GameObject gameManager;
public int happiness = 0;
public int sadness = 0;
public int musicHappiness = 0;
public Material happyMaterial;
public Material neutralMaterial;
public Material sadMaterial;
// Use this for initialization
void Start()
{
gameManager = GameObject.FindGameObjectWithTag("GameManager");
print("script working");
}
// Update is called once per frame
void Update()
{
if (gameManager.GetComponent<ManageScript>().complimentBoy == true)
{
happiness = happiness + 1;
sadness = sadness - 1;
gameManager.GetComponent<ManageScript>().complimentBoy = false;
}
if (gameManager.GetComponent<ManageScript>().insultBoy == true)
{
sadness = sadness + 1;
happiness = happiness - 1;
gameManager.GetComponent<ManageScript>().insultBoy = false;
}
if (gameManager.GetComponent<ManageScript>().leaveBoy == true)
{
print("Left Boy");
gameManager.GetComponent<ManageScript>().leaveBoy = false;
}
if (happiness >= sadness)
{
GetComponent<Renderer>().material = happyMaterial;
}
if (happiness <= sadness)
{
GetComponent<Renderer>().material = sadMaterial;
}
if (happiness == sadness)
{
GetComponent<Renderer>().material = neutralMaterial;
}
if (gameManager.GetComponent<ManageScript>().musicIsOn == true)
{
musicHappiness = 1;
happiness = happiness + musicHappiness;
gameManager.GetComponent<ManageScript>().musicIsOn = false;
}
if (gameManager.GetComponent<ManageScript>().musicIsOff == true)
{
musicHappiness = -1;
happiness = happiness + musicHappiness;
gameManager.GetComponent<ManageScript>().musicIsOff = false;
}
}
void OnMouseDown()
{
print("Talking is working");
gameManager.GetComponent<ManageScript>().freezeMove();
gameManager.GetComponent<ManageScript>().boyTalk();
}
}