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
0
Question by kl852 · May 07, 2021 at 10:29 PM · scripting problemgameobject2d game

How to make an object invisible before it is triggered to show?

Hi,

I am working on a 2D mini game.

When the character (player) walks into an invisible trigger, a game object will be shown (and that will be destroyed with Space Bar).

The reason of setting a trigger for each game object is to avoid 1. The player seeing all the game objects too early 2. All the objects being destroyed when the player pushes the space bar for the first time.

My Script for the invisible trigger is successful, however, the game object doesn't go invisible even when the "Sprite Renderer" box under the Inspector has been unchecked- once I start playing the game, it's checked.

I have added a separate under the game object to disable the sprite renderer, however, with that, the object doesn't show.

Thanks in advance!


trigger for the game object to show: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class GameObjShow : MonoBehaviour  
 {
     public GameObject customImage;
     private SpriteRenderer rend;
 
     // Start is called before the first frame update
     void Start()
     {
         customImage.SetActive(false);
         rend = customImage.GetComponent<SpriteRenderer>();
         rend.enabled = false;
     }
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Player");
         {
             customImage.SetActive(true);
             Debug.Log("On trigger enter 2D");
             rend.enabled = true;
         } 
     }
     // Update is called once per frame
     void Update()
     {
     }
 }
 

sprite renderer disabled for the game object:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpriteReaction : MonoBehaviour
 {
     SpriteRenderer rend;
 
     // Start is called before the first frame update
     void Start()
     {
         rend = GetComponent<SpriteRenderer>();
     }
 
     // Update is called once per frame
     void Update()
     {
         this.rend.enabled = false;
     }
 }


Thanks!

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
0
Best Answer

Answer by GSGregory · May 08, 2021 at 12:03 AM

I think the issue is that in the OnTriggerEnter2D you call rend, but do not set it. It looks like an issue of scope. I believe this should work, or declare rend = inside of the onTriggerEnter as well private SpriteRenderer rend = customImage.GetComponent<SpriteRenderer>();

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 kl852 · May 08, 2021 at 09:38 PM 0
Share

Thank you so much for your advice, I have added it under onTriggerEnter2D, and replaced all "SpriteRenderer" in both scripts, and in the inspector of the gameObject I have unchecked the box next to the item name, now I achieved the desired command!

avatar image
0

Answer by FrozenTrident · May 09, 2021 at 04:27 AM

I think that you just need to uncheck the object in the inspector and then when you need it you can activate it again with "yourGameObject.SetActive(true);"

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

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

214 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

Related Questions

Find Tilemap Player is Currently At 0 Answers

Spawn an object with additional parameter 1 Answer

Unity 3D: How to make the gameobject speed increase continuously on tapping quickly? 1 Answer

Script wont let me drag and drop GameObject Of UI Menu into it? 1 Answer

Destroy script turns objects into ghosts 2 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