Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KCMDragon · Dec 27, 2017 at 11:46 PM · scripting probleminstantiateprefabsnot workingsimulation

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();
 }


}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 .

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KCMDragon · Dec 28, 2017 at 03:47 AM 0
Share

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.

avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image siscoartz · Jan 28, 2019 at 04:11 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

192 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges