• 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 RandomCharacters · Oct 26, 2015 at 03:43 AM · errorinstantiate

I get an issue after instantiating and assigning a variable game object

I have a main menu. When you click a button it changes to a new terrain/water/land. I destroy the old game object (with everything in it) and create a new one. Inside the new one are tags 'water', 'terrain' and 'land'. It is verified the tags are there and correctly addressed.

When I load the scene, the FIRST time the game runs, it correctly displays the water/terrain/land. However, if I try to turn any of those off, it gives and error. In the inspector, the public variable is correct.

When I press the button, it should destroy the old game object, load the new one in with tags, look at the tags and assign them so that they can be turn on or off. It is not doing this. I try to assign the tags after the new object is instantiated but sometimes it does not assign correctly. As I said, the FIRST time it assigns properly and after that it does not.

I am thinking that after instantiating the new object, the assign of the tag should work. it doesn't seem to be doing that. (the first part is repeated 6 times foe each of the game objects to load. They are the same) Any ideas?

 public class MainMenu : MonoBehaviour
 {
     //////////////////////////////////////////////////////////////////////////////////////
     Settings settingsClass;                 // script reference
     //////////////////////////////////////////////////////////////////////////////////////
       public GameObject land;
     public GameObject water;
     public GameObject mountainsObject;
     public Terrain mountainsEmpty;
   void Start()
     {
            ChangeTerrain();                              
            ChangeEnvironment();
 //////////////////////////////////////////////////////////////////////////////////////
      public void ChangeTerrain()
     {
         if (Set.nameOfCourseInt == 1)
         {
             if (GameObject.FindWithTag(Set.nameOfCourseStr1))
             {
                 ////////////////////////////////////////////////
                 //////////
                 //Rockbania Flats
                 //////////
                 if (GameObject.FindWithTag("RockbaniaS"))
                 {
                     Destroy(GameObject.FindWithTag("RockbaniaS"));
                 }
                 ////
                 if (GameObject.FindWithTag("Volcania"))
                 {
                     Destroy(GameObject.FindWithTag("Volcania"));
                 }
                 ////
                 if (GameObject.FindWithTag("Greenlandia"))
                 {
                     Destroy(GameObject.FindWithTag("Greenlandia"));
                 }
                 ////
                 if (GameObject.FindWithTag("Icelandia"))
                 {
                     Destroy(GameObject.FindWithTag("Icelandia"));
                 }
                 
                 ////////////////////////////////////////////////
                 course1Settings.SetActive(true);    //Flats of Rockbania
                 course2Settings.SetActive(false);   //Slopes of Rockbania
 
                 course3Settings.SetActive(false);   //Volcania
                 course4Settings.SetActive(false);   //Greelandia
                 course5Settings.SetActive(false);   //Icelandia
 
                 settingsClass = GameObject.FindWithTag(Set.nameOfCourseStr1).GetComponent<Settings>();     // get script
                 settingsClass.ResetSettings();                                                             // reset game settings
 
                 text1.text = "Flats of Rockbania";
                 ////
                 if (!GameObject.FindWithTag("RockbaniaF"))
                 {
                     Instantiate(courseRockbaniaF, Vector3.zero, Quaternion.identity);           
                 }
             }
         }
         ////////////////////////////////////////////////
         else if (Set.nameOfCourseInt == 2)
         {
             if (GameObject.FindWithTag(Set.nameOfCourseStr2))
             {
                 ////////////////////////////////////////////////
                 if (GameObject.FindWithTag("RockbaniaF"))
                 {
                     Destroy(GameObject.FindWithTag("RockbaniaF"));
                 }
                 //////////
                 //Rockbania Slopes
                 //////////
                 if (GameObject.FindWithTag("Volcania"))
                 {
                     Destroy(GameObject.FindWithTag("Volcania"));
                 }
                 ////
                 if (GameObject.FindWithTag("Greenlandia"))
                 {
                     Destroy(GameObject.FindWithTag("Greenlandia"));
                 }
                 ////
                 if (GameObject.FindWithTag("Icelandia"))
                 {
                     Destroy(GameObject.FindWithTag("Icelandia"));
                 }                
                 ////////////////////////////////////////////////
                 course1Settings.SetActive(false);
                 course2Settings.SetActive(true);
 
                 course3Settings.SetActive(false);
                 course4Settings.SetActive(false);
                 course5Settings.SetActive(false);   //Icelandia
 
                 settingsClass = GameObject.FindWithTag(Set.nameOfCourseStr2).GetComponent<Settings>();     // get script
                 settingsClass.ResetSettings();                                                             // reset game settings
 
                 text1.text = "Slopes of Rockbania";
                 ////
                 if (!GameObject.FindWithTag("RockbaniaS"))
                 {
                     Instantiate(courseRockbaniaS, Vector3.zero, Quaternion.identity);
                 }
             }
         }
         ////////////////////////////////////////////////
                    /////
         //Set objects from tags
             land = GameObject.FindWithTag("land");
             water = GameObject.FindWithTag("water");
             mountainsObject = GameObject.FindWithTag("terrain");
         /////
     }
     /////////
     public void ChangeEnvironment()
     {
         /////
             Debug.Log(environmentInt);
             Debug.Log(land);
             Debug.Log(water);
             Debug.Log(mountainsObject);
         ////////////////////////////////////////////////
         switch (environmentInt)
         {
             case 4: //water on, mountains on     
                 land.gameObject.SetActive(false);
                 water.gameObject.SetActive(true);
                 mountainsObject.gameObject.SetActive(true);
                 mountainsEmpty = mountainsObject.GetComponent<Terrain>();
                 mountainsEmpty.enabled = true;
 
                 text2.text = "Water/Lava & Mountains";
 
                 break;
 
             case 1: //land only
                 land.gameObject.SetActive(true);
                 water.gameObject.SetActive(false);
                 mountainsObject.gameObject.SetActive(false);
                 mountainsEmpty = mountainsObject.GetComponent<Terrain>();
                 mountainsEmpty.enabled = false;
 
                 text2.text = "Land Only";
 
                 break;
 
             case 2:  //water/lava only
                 land.gameObject.SetActive(false);
                 water.gameObject.SetActive(true);
                 mountainsObject.gameObject.SetActive(false);
                 mountainsEmpty = mountainsObject.GetComponent<Terrain>();
                 mountainsEmpty.enabled = false;
 
                 text2.text = "Water/Lava Only";
 
                 break;
 
             case 3:  //Land on, mountans on
                 land.gameObject.SetActive(true);
                 water.gameObject.SetActive(false);
                 mountainsObject.gameObject.SetActive(true);
                 mountainsEmpty = mountainsObject.GetComponent<Terrain>();
                 mountainsEmpty.enabled = true;
 
                 text2.text = "Land & Mountains";
 
                 break;
 
             default: //water on, mountains on
                 environmentInt = 4;
                 land.gameObject.SetActive(false);
                 water.gameObject.SetActive(true);
                 mountainsObject.gameObject.SetActive(true);
                 mountainsEmpty = mountainsObject.GetComponent<Terrain>();
                 mountainsEmpty.enabled = true;
 
                 text2.text = "Water/Lava & Mountains";
 
                 break;
         }
     }
     /////////

 //////////////////////////////////////////////////////////////////////////////////////
     //p2 Change Course Button
     public void OnCourseBtn()
     {
         //////////
         audio2.PlayOneShot(menuSound10, 1f);
         //////////////////////////////////////////////////////////////////////////////////////
         Set.nameOfCourseInt = Set.nameOfCourseInt + 1;    //First increment course name by 1        
 
         if (Set.nameOfCourseInt < 1)
         {
             Set.nameOfCourseInt = 1;
         }
         //////////
         if (Set.nameOfCourseInt > 5)
         {
             Set.nameOfCourseInt = 1;
         }
 
         text2 = courseText.GetComponent<Text>();
         //////////////////////////////////////////////////////////////////////////////////////
         if (Set.nameOfCourseInt == 1)
         {
             course1Settings.SetActive(true);
             course2Settings.SetActive(false);
 
             course3Settings.SetActive(false);
             course4Settings.SetActive(false);
             course5Settings.SetActive(false);   //Icelandia
 
             Set.nameOfCourseStr = "settings-1";
 
             settingsClass = GameObject.FindWithTag(Set.nameOfCourseStr).GetComponent<Settings>();         // get script
             settingsClass.ResetSettings();                                                                 // reset game settings
 
             text2.text = "Flats of Rockbania";
 
         }
         //////////////////////////////////////////////////////////////////////////////////////
         else if (Set.nameOfCourseInt == 2)
         {
 //////////////////////////////////////////////////////////////////////////////////////
     //p2 ENVIRONMENT Button
     public void OnEnvironmentBtn()
     {
         ///////////////////////////////////////////
             audio2.PlayOneShot(menuSound10, 1f);
         ///////////////////////////////////////////
         //Sets Envirnonment Up with name
             text3 = environmentText.GetComponent<Text>();
         ///////////////////////////////////////////
             environmentInt = environmentInt + 1;
         ///////////////////////////////////////////
             if (environmentInt < 1 || environmentInt > 4)
             {
                 environmentInt = 1;
             }
         ///////////////////////////////////////////
             ChangeEnvironment();
         ///////////////////////////////////////////
         // Save to disk
          ES2.Save(environmentInt, "keyEnvironmentInt");
     }
     //////////////////////////////////////////////////////////////////////////////////////
 
 

capture2.png (5.3 kB)
capture.png (6.1 kB)
Comment
Add comment · Show 3
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 RandomCharacters · Oct 26, 2015 at 03:44 AM 0
Share

alt text

alt text

capture2.png (5.3 kB)
capture3.png (469.6 kB)
avatar image RandomCharacters · Oct 26, 2015 at 03:53 AM 0
Share

alt text

capture4.png (37.9 kB)
avatar image RandomCharacters · Oct 26, 2015 at 04:37 AM 0
Share

I was thinking. $$anonymous$$aybe after instantiating the code blows past the assign tags so they don't get assigned. $$anonymous$$aybe somehow put the instantiate and assign tags in a coroutine.

Anyone?

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

34 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

Related Questions

Instantiate() Argument error 1 Answer

Trying to replace 2 objects. Destroying assets is not permitted to avoid data loss. 0 Answers

Instantiated button prefab causes Delegation error on click and does not add OnClick Listener 0 Answers

*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer

SpaceShooter Tutorial instanced asteroid not taking GameController game object in unity5 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