Prefab Scripts Not Working After First Spawn?
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();
}
}
Answer by ransomink · Dec 28, 2017 at 02:05 AM
I would check when stop
becomes true since that disables the while loop which spawns the game objects as I can't see why it stops after two spawns.
Also, you can save yourself a lot of performance by caching the ManageScript and the Renderer component (you cache a reference to gameManager
as a game object). You're trying to access its ManageScript.
It's better to change this: public GameObject gameManager;
to this: public ManageScript gameManager
ManageScript gameManager;
Renderer renderer;
void Start()
{
gameManager = GameObject.FindGameObjectWithTag( "GameManager" ).GetComponent<ManageScript>();
renderer = GetComponent<Renderer>();
}
Then you can easily access the variables gameManager.complimentBoy == true
or renderer.material = sadMaterial
.
Thank you! This really helps. ^-^ I'll try some of your advice out. However, its not that prefab stops spawning, its just the particular script inside of it stops working.
Answer by LOSTSOUL86 · May 05, 2018 at 08:40 AM
Hi I have similiar problem. I made a prefab and then within the game I duplicate the prefab and put it in the scene. Prefabs appear in the Scene - physics works but script attached to prefab is not executed. I put simple debug.log in start / update functions to check, but non of them were called. Any Ideas?
The same thing is happening to me, the original game object that I turned into a prefab works, but any spawning prefabs, all the attached scripts are broken!
Your answer
Follow this Question
Related Questions
How can I in-script create gameobject from prefab? 2 Answers
Ragdoll prefabe is not working 0 Answers
Destroy a prefab from another class 2 Answers
Procedurally generating cubes 1 Answer
How to get the position of a new Instantiated prefab 0 Answers