• 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 mortezadianatfar · Apr 21, 2020 at 10:13 AM · playerboxcollider

How i can make object hide and visible by collision with other object?

I make a cube object and attach main camera to the cube. Also I have another cube and cylinder in the area, so when I play, they should be invisible and after the cube or player has contact with object, it gets visible:

public class Player : MonoBehaviour { // Start is called before the first frame update private float _speed = 1000f; public GameObject yellow; public GameObject red;

 void Start()
 {
     yellow.gameObject.SetActive(false);
     red.gameObject.SetActive(false);
     
 }
 

 // Update is called once per frame
 void Update()
 { 
     CalculateMovement();
 }
 void CalculateMovement()
 {
     float horizontalInput = Input.GetAxis("Horizontal");
     float verticalInput = Input.GetAxis("Vertical");
     float x = 5f * Input.GetAxis("Mouse X");
     float y = 5f * -Input.GetAxis("Mouse Y");
     

     Vector3 direction = new Vector3(horizontalInput, 0, verticalInput);

     transform.Translate(direction * _speed * Time.deltaTime);
     transform.Rotate(y,x,0);
     transform.rotation = Quaternion.Euler(transform.eulerAngles.x,transform.eulerAngles.y,0);
 }
 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Collaborative_area"))
     {
         other.gameObject.SetActive(true);
     }
     if (other.gameObject.CompareTag("Dangerous_area"))
     {
         red.gameObject.SetActive(true);
     }
 }


}

And I get confused for which object I should define collider and rigidbody?

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

Answer by servo150 · Apr 21, 2020 at 10:56 AM

The main error I see there is that you disable red and yellow (in the Start method). So you disable the whole GameObject, including its child game objects and every component attached to them. This is why OnTriggerEnter never fires for red and yellow.

Instead of disabling them in the Start method, I would write something like red.GetComponent<MeshRenderer>().enabled = false inside the Start method, and red.GetComponent<MeshRenderer>().enabled = true inside the OnTriggerEnter method, in the if statement.

And about your question about colliders and rigidbody settings, I suggest you take a look at https://docs.unity3d.com/Manual/CollidersOverview.html?_ga=2.30589565.330322998.1587295408-896355190.1566342102 At the very bottom of this page, you'll find a table that shows what combination of collider and rigidbody settings trigger what functions.

Hope this helps !

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 mortezadianatfar · Apr 21, 2020 at 01:52 PM

Yes, thanks it works. I have an another questions, I use my yellow cube as transparent material, I want to when I enter to the object it gets visible and show it in game mode. The visibility works by your guidance, as you can see in image it show up in the scene but when enters the object , it disappears from game mode. Somehow it looks like it just show surfaces! alt text


capture.jpg (158.9 kB)
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

156 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

Related Questions

Help with player controller 0 Answers

Why does my player keep falling through the floor? 2 Answers

2D player stuck between BoxColliders 1 Answer

Player have to stop when hit a corner 0 Answers

Push enemy off map for points? 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