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
4
Question by aagrlp640 · Aug 25, 2013 at 08:12 PM · gameobjectactiveinactive

Activate gameobject that is deactivated with c#

Hi, I have a gameobject called Trees that have some trees inside, it begins inactive or I also can disable through this code in C#:

 if(Input.GetKeyUp(KeyCode.Space)){         
    GameObject.Find("Trees").active = false;
 }

But when i want to reactivate it with:

 GameObject.Find("Trees").active = true;

unity can find the gameobject, is there a way to find it and activate it again?

alt text

question.png (22.4 kB)
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
6
Best Answer

Answer by cmpgk1024 · Aug 25, 2013 at 08:15 PM

 public GameObject Trees;
 
 void Update(){
    if(Input.GetKey(KeyCode.Space)){
        Trees.SetActive(false);
    }
    else{
      Trees.SetActive(true);
    }
 }

set trees in the inspector.

Comment
Add comment · Show 6 · 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 aagrlp640 · Aug 25, 2013 at 09:11 PM 0
Share

Thanks for your answer!! :D sorry but im new in this of unity :( how can i set trees in the inspector?

avatar image cmpgk1024 · Aug 26, 2013 at 01:20 AM 0
Share

Drag your Trees GameObject into the serialized field Trees that will appear in the inspector.
Example of a serialized field: serialized field example
I realized I left out a brace in my code, I'll edit it.

serializedfield.png (8.0 kB)
avatar image aagrlp640 · Aug 26, 2013 at 03:26 AM 0
Share

Thanks for your help I found this solution

     foreach (GameObject go in GameObject.FindGameObjectsWithTag ("TreeTag")) {
         go.renderer.enabled = enabled;    
 }

Also work as you said :D

avatar image cmpgk1024 · Aug 26, 2013 at 12:18 PM 0
Share

The way you are doing it doesn't disable the GameObject - it just makes it invisible. If you want to change whether it is enabled, use SetActive() with true or false.

avatar image aagrlp640 · Aug 26, 2013 at 06:44 PM 0
Share

Can you send an example? its cause when i try to reactivate an gameboject y cant be restore, i read in some places that after inactive unity wont recognize it

avatar image mattshu aagrlp640 · Nov 11, 2017 at 04:20 PM 0
Share

I'm having the same issue. If you uncheck a gameobject in the inspector, I can't re-enable it with a script using .SetActive()

avatar image
0

Answer by madhu_unity425 · Mar 22, 2019 at 12:59 PM

i need without public or serializable field

could you please help me for that

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 unity_W0xCvbs1Eqytzw · Apr 22, 2020 at 09:04 PM 1
Share

Try putting the key word [Serialize Field] above it.

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

19 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

Related Questions

Toggling a game object between an active and inactive state 1 Answer

Finding all GameObjects with the same tag when some of them are inactive? 4 Answers

How to find Inactive GameObject 16 Answers

GameObject.Find() work on inactive objects 16 Answers

How to use if statement properly 1 Answer


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