Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 15, 2015 at 02:13 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by JayFitz91 · Feb 15, 2015 at 12:46 PM · gameobjectsetactive

Problem activating an object

I'm having an issue re-enabling a gameObject, I have the following code

 using UnityEngine;
 using System.Collections;
 
 public class tileColorChange : MonoBehaviour {
 
     private GameObject fireField;
 
     private GameObject battleArena;
 
     private GameObject fireTerrain;
 
 
     void Start()
     {
         battleArena = GameObject.FindGameObjectWithTag("battleArena");
 
         //Find the fireTerrain GameObject
         fireTerrain = GameObject.FindGameObjectWithTag("fireTerrain");
 
         //Returns the fireTerrain gameObject as expected
         Debug.Log(fireTerrain)
         
         //Disable the gameObject, this works fine
         fireTerrain.SetActive(false);
 
     }
 
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "rusty" || col.gameObject.tag == "wheels" || col.gameObject.tag == "bulbus")
         {
             if(gameObject.tag == "fire")
             {
                 gameObject.renderer.material.color = Color.red;
                 fireField = Instantiate(Resources.Load("Lava")) as GameObject;
                 fireField.transform.parent = battleArena.transform;
                 fireField.transform.position = battleArena.transform.position + new Vector3(0.0f, 0.1f, 12.0f);
 
                 //Shows as NULL
                 Debug.Log(fireTerrain);
                 
                 //This fails as a result
                 fireTerrain.SetActive(true);
                 StartCoroutine(raise());
             }
 
         }
     }
 
     void OnTriggerExit()
     {
         gameObject.renderer.material.color = Color.black;
         Destroy(fireField);
 
         StartCoroutine(lower());
     }
 
     IEnumerator raise()
     {
         if (gameObject.tag == "fire")
         {
             while (fireTerrain.transform.position.y <= 0)
             {
                 fireTerrain.transform.position += new Vector3(0.0f, 0.1f, 0.0f);
                 yield return null;
             }
         }
 
     }
 
     IEnumerator lower()
     {
         if (gameObject.tag == "fire")
         {
             while (fireTerrain.transform.position.y >= -13)
             {
                 fireTerrain.transform.position -= new Vector3(0.0f, 0.1f, 0.0f);
                 yield return null;
                 if (fireTerrain.transform.position.y <= -13)
                 {
                     fireTerrain.SetActive(false);
                 }
             }
         }
     }
 }

I have commented where I am having an issue but basically, I disable a gameObject in the start function, I then want to re-enable it when I stand on another tile gameObject I have in the scene.

In the Start function, the gameObject is set to fireTerrain, which is correct. But during the OnTriggerEnter function, when I debug the object again, it shows as NULL this time.

If anyone could show me where I'm going wrong, I'd appreciate it

Comment
Add comment · Show 2
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 Xoduz · Feb 15, 2015 at 01:17 PM 0
Share

I'm not sure what caused your error, but when I adjusted your code to leave in only the bits directly related to the fireTerrain, my dummy-fireTerrain was activated & raised on trigger enter and lowered & deactivated on trigger exit. The code I tested with:

 using UnityEngine;
 using System.Collections;
  
  public class tileColorChange : $$anonymous$$onoBehaviour {
  
      private GameObject fireTerrain;
  
      void Start()
      {
          fireTerrain = GameObject.FindGameObjectWithTag("fireTerrain");
          fireTerrain.SetActive(false);
      }
  
      void OnTriggerEnter(Collider col)
      {
          fireTerrain.SetActive(true);
          StartCoroutine(raise());
      }
  
      void OnTriggerExit()
      {
          StartCoroutine(lower());
      }
  
      IEnumerator raise()
      {
          while (fireTerrain.transform.position.y <= 0)
          {
              fireTerrain.transform.position += new Vector3(0.0f, 0.1f, 0.0f);
              yield return null;
          }
      }
  
      IEnumerator lower()
      {
          while (fireTerrain.transform.position.y >= -13)
          {
              fireTerrain.transform.position -= new Vector3(0.0f, 0.1f, 0.0f);
              yield return null;
              if (fireTerrain.transform.position.y <= -13)
              {
                  fireTerrain.SetActive(false);
              }
          }
      }
  }
avatar image JayFitz91 · Feb 15, 2015 at 02:05 PM 0
Share

Thanks, I tried emulating your method but had no results unfortunately, I managed to get it working though using if statements in the start function, thanks for the help :)

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by JayFitz91 · Feb 15, 2015 at 02:07 PM

Managed to get it working, using if statements in the start function to identify the correct tile, everything works accordinly now, thanks for the help :)

 using UnityEngine;
 using System.Collections;
 
 public class tileColorChange : MonoBehaviour {
 
     private GameObject fireField;
 
     private GameObject battleArena;
 
     private GameObject fireTerrain;
 
 
     void Start()
     {
         battleArena = GameObject.FindGameObjectWithTag("battleArena");
 
         if (gameObject.tag == "fire")
         {
             fireTerrain = GameObject.FindGameObjectWithTag("fireTerrain");
             fireTerrain.SetActive(false);
         }
 
     }
 
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "rusty" || col.gameObject.tag == "wheels" || col.gameObject.tag == "bulbus")
         {
             if(gameObject.tag == "fire")
             {
                 gameObject.renderer.material.color = Color.red;
                 fireField = Instantiate(Resources.Load("Lava")) as GameObject;
                 fireField.transform.parent = battleArena.transform;
                 fireField.transform.position = battleArena.transform.position + new Vector3(0.0f, 0.1f, 12.0f);
 
                 //Shows as fireTerrain GameObject now
                 Debug.Log(fireTerrain);
                 
                 //Everything works as normal
                 fireTerrain.SetActive(true);
                 StartCoroutine(raise());
             }
 
         }
     }
 
     void OnTriggerExit()
     {
         gameObject.renderer.material.color = Color.black;
         Destroy(fireField);
 
         StartCoroutine(lower());
     }
 
     IEnumerator raise()
     {
         if (gameObject.tag == "fire")
         {
             while (fireTerrain.transform.position.y <= 0)
             {
                 fireTerrain.transform.position += new Vector3(0.0f, 0.1f, 0.0f);
                 yield return null;
             }
         }
 
     }
 
     IEnumerator lower()
     {
         if (gameObject.tag == "fire")
         {
             while (fireTerrain.transform.position.y >= -13)
             {
                 fireTerrain.transform.position -= new Vector3(0.0f, 0.1f, 0.0f);
                 yield return null;
                 if (fireTerrain.transform.position.y <= -13)
                 {
                     fireTerrain.SetActive(false);
                 }
             }
         }
     }
 }

Comment
Add comment · 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
0

Answer by awplays49 · Feb 15, 2015 at 01:57 PM

I had a similar issue. If its not activating the second time, It could mean some of the activation code crucial to its activation is in the deactivated object. Remember that when an object is deactivated, Their scripts dont work.

Also change

 private GameObject fireTerrain;

to

 public Terrain fireTerrain;

and drag it in. Then delete the part where you "find" it. Do this for all of the ones you "found", because GameObject.Find is extremely SLOW. Use public variables to reference objects and things like so.

Comment
Add comment · Show 3 · 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 JayFitz91 · Feb 15, 2015 at 02:05 PM 0
Share

Hi, thanks for the input, but my fireTerrain is not actually a terrain but a gameObject I made in 3ds max that emulates a lava field, I should probably name it better :P

The issue with using public is I have 6 objects with this script and 6 tiles, which means I would have to drag 6 objects onto each tile, resulting in 36 different objects, I find it would be easier to do it with Find, as it only happens in the Start function, it shouldn't be too slow

avatar image awplays49 · Feb 15, 2015 at 02:08 PM 0
Share

Yea good idea. Im almost positive your gameobject, though, is just deactivated and cant respond. is there any part of it that plays a role in its activation? what youre describing sounds like it has something on it that needs to be used to deactivate.

avatar image JayFitz91 · Feb 15, 2015 at 02:11 PM 0
Share

I'm not too sure what caused it but, I set it up to check for its tag in the if statement so it loads it there, I then deactivate it immediately after and it seems to work. I have more than fireTerrain, ive other Terrains too, I just used one for this question, I'm guessing it must have been trying to find all 6, either way, its running now and everything seems ok.

Thanks again for the help :)

Follow this Question

Answers Answers and Comments

21 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

Related Questions

c# Set Active error, conflicting with other code.. 1 Answer

GameObjects that I deactivate are reactivated immediately 0 Answers

SetActive(true) not working 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Panel GameObject not activating 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