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 /
avatar image
1
Question by ramonfr · Dec 17, 2011 at 07:53 PM · gameobjectphysicstransform

How to reactivate a gameObject?

Once I set transform.gameObject.active = false, I can't set it back to transform.gameObject.active = true. Why?

I've tried making a different function, if loops, while loops, coroutine, and nothing works.

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

3 Replies

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

Answer by flamy · Dec 17, 2011 at 08:38 PM

U cannot reactivate it ! The reason is once u set the game object's enable to false, the script and other components stops working, u cannot reactivate it from the same script or any of the scripts that is attached to that particular game object...

you should have the reference to the game object outside somewhere and then u can control the enable as u like! I hope it helps :)

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 ramonfr · Dec 17, 2011 at 09:27 PM 0
Share

Thanks, it's working. But now I have a logic problem.

avatar image
1

Answer by Tasarran · Dec 18, 2011 at 07:09 AM

Just take it back one step...

Make an empty GameObject and put the object you want to enable/disable grouped as a child.

Take the script you were using on the now child object and put it on the parent.

You can declare a GameObject var in this script, and attach the child to it in the Inspector. Then, enable/disable this child object, instead of just using gameObject.active = false.

This way, your invisible parent object is always active to control the state of the actual object you want to toggle.

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 Imagineum · Aug 29, 2014 at 06:46 PM

I did it that way and it works but my child does't move anymore.

Here's a code that disable my additional enemy on start and reactivate it when player has 200 points.

 using UnityEngine;
 using System.Collections;
 
 public class EnemyActivatorController2 : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         GameObject ActiveEnemy2 = GameObject.Find ("EnemyActivator2");
         foreach (Transform child in ActiveEnemy2.transform) {
                         child.gameObject.SetActive (false);
                 }
 
 
     }
     
     // Update is called once per frame
     void Update () {
         if (PlayerController.Points == 200) {
             GameObject ActiveEnemy2 = GameObject.Find ("EnemyActivator2");
             foreach (Transform child in ActiveEnemy2.transform) {
                 child.gameObject.SetActive (true);
             } 
         }
     
     }
 }

It working but my additional enemy spawn and doesn't move anymore.

Here is enemy script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyController2 : MonoBehaviour {
 
     public float speed = -1;
     private Transform spawnPoint;
 
     // Use this for initialization
     void Start () {
         spawnPoint = GameObject.Find("SpawnPoint").transform;
         rigidbody2D.velocity = new Vector2 (speed, 0);
 
 
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnBecameInvisible()
     {
         if (Camera.main == null)
                         return;
 
         float yMax = Camera.main.orthographicSize - 0.5f;
         transform.position = new Vector3( spawnPoint.position.x,
                                          Random.Range (-yMax, yMax),
                                          transform.position.z );
     }
 
 }
 
 

Any ide how to make he move again?

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 TimB · Jun 19, 2015 at 04:48 PM 0
Share

This is very late, but for anyone who runs into this. You need to move your velocity setting out of Start() and into OnEnable(). Start is only called once, OnEnable will be called every time you set your object as active.

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

6 People are following this question.

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

Related Questions

Assign a 'Transform' target to prefab by finding a 'GameObject' 1 Answer

Rotate camera around gameobject 1 Answer

Translate.transform problems (collision/Rigibody) 0 Answers

How to delay a line of code. 2 Answers

Deploy object avoiding collisions 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