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 bobjtls · Mar 26, 2019 at 03:07 AM · scripting problem

Can not show/hide a game object in Unity 3D V2018.3.9 f1

I have been trawling the the answer to this issue but can not find any way to get my script to hide/show a gameobject. I have tried many of the suggestions in other answers but to no avail. What am I doing wrong?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Artwork : MonoBehaviour
 {
     public SpriteRenderer artwork;
     public int countsincestartup = 20;
     public int numberofChildren;
     private Rigidbody2D rb;
     private int movingChildren;
     private GameObject child;
     private string childName;
     private int startCounter = 0;
 
 
     // Start is called before the first frame update
     void Start()
     {
         artwork = GetComponent<SpriteRenderer>();
         artwork.sortingOrder = 1;
     }
 
     private void Update()
     {
         //hide artwork after specified number of cycles
 
          if (startCounter > countsincestartup)
         {
             gameObject.GetComponent<SpriteRenderer>().enabled = false;
 
         }
         else {
             startCounter += 1;
             }
 
         //show artwork when all objects have stopped moving
         movingChildren = numberofChildren;
 
             for (int i = 1; i <= numberofChildren; i++)
             {
                 if (i < 10)
                 {
                     childName = artwork.name.Replace("_00", "_0" + i.ToString());
                 }
                 else
                 {
                     childName = artwork.name.Replace("_00", "_" + i.ToString());
             }
             child = GameObject.Find(childName);
             rb = child.GetComponent<Rigidbody2D>();
             var childVelocity = rb.velocity;
             if (childVelocity.sqrMagnitude < 0.01f)
                 {
                     movingChildren -= 1;
                 }
             }
             if (movingChildren == 0)
             {
             //all movement stopped
             gameObject.GetComponent<SpriteRenderer>().enabled = true;
         }
        
     }
 
 }
 
 

Comment
Add comment · Show 1
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 bobjtls · Mar 26, 2019 at 04:32 AM 0
Share

I wonder if this issue persists because he object I am trying to show or hide is a child object? Further investigations through the forums seems to indicate that I may have to call child objects differently from parent objects. Another project I tried earlier successfully used the game object.enable code to show and hide but this was on a parent object with no children.

Will move the child to a parent position and see.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by dan_wipf · Mar 26, 2019 at 04:13 AM

i believe debugin your code, will get you to the desired result +> is the code stuck some where in an if statement and so on.. try use Debug.Log(=> something) to see if the of statements are acually working.

Comment
Add comment · Show 2 · 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 bobjtls · Mar 26, 2019 at 04:36 AM 0
Share

Dan_wipf, I have used Debug.Log in various places to ensure that the counter in incremented and that the if statement passes for hiding the game object. In both cases the answer is yes, the counter does increment and processing enters that part of the if statement to hide the gameobject.

As per my comment to made just prior to answering your suggestion, I will investigate if this is something to do with the game object being a child object of another gameobject.

Thanks for your suggestion though, it is appreciated.

avatar image dan_wipf bobjtls · Mar 26, 2019 at 04:56 AM 0
Share

well you might have messed up disabling the GameObject, with disabling the SpriteRenderer Component.


if you only disable the SpriteRenderer attached to the GameObject, the children Objects are still shown.


to really disable a GameObject you Need to call this: gameObject.SetActive(true/false);


this will result in as i believe you know that the gameObject and all it's children will be disabled/enabled in the hirarchy and all the Script's will be unaccessable due to disabling gameObject.

avatar image
0

Answer by Vollmondum · Mar 26, 2019 at 05:26 AM

Best way to hide sprites is to assign empty material(image) and never mess up with enabling the renderer.

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 bobjtls · Mar 26, 2019 at 09:49 AM 0
Share

Vollmondum, thanks for the suggestion. I'll give it a try and see how this works.

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

244 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 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

Unity 5 use PushAssetDependencies and PopAssetDependencies 0 Answers

Performance peaks in ScriptRunBehaviourUpdate 1 Answer

Inherance script problem [C#] 0 Answers

Help with retroactive score multiplier 2 Answers

How would I generate a new object based on player location? 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