- Home /
How to hide a Game Object on collission?
Heys guys,
I hope somone can help me with this:
I have a Game Object "A" and a RigidBodyFPSController with a Box Collider "B" with Mesh Renderer turned off. When the Player collides with "B" I want to deactivae/hide "A" and as soon as the Player exits the Box Collider "B" the Game Object "A" should be visible again.
Who has mercy with my noob-question and can provide a script for that? You would make my day!
Many thanks!
Best John
Answer by awesomekev · Apr 25, 2018 at 06:14 PM
To read more about collision, I can recommend this tutorial. If you just want to hide "A" you can simply also deactivate the render on collision.
A.GetComponent<Renderer> ().enabled = false;
@awesomekev thank you very much for your quick respond. yes, that´s exactly what I would like to achieve - Object "A" should only be deactivated while the Player is collided with "B"
I am still struggling with the correct code. I have checked your tutorial-link as well as the documentation and tried your line of code, but I haven´t achieved the goal.
I am new to Unity and looks like I am doing something wrong. Could someone show me what am I doing wrong here?
Which results in this error: "Can´t add script component "RenderOff" because the script class cannot be found. $$anonymous$$ake sure that there are no compile errors and that the file nam and class name match.
Here is the Code I try to attach.
using UnityEngine; using System.Collections;
public class DestroyCubes : $$anonymous$$onoBehaviour { void OnCollisionEnter (Collision col) { if(col.gameObject.name == "B") { Destroy(col.gameObject); } } }
On my Object "B" I have a Box Collider with Trigger turned to on
I would really appreciate some help here!
For me the error sounds like as if your script name does not match your file name. That means your script class' current name is DestroyCubes then your file name must be DestroyCubes.cs. Is that the case? They have to match for unity to work.
Answer by JohnBrick · Apr 29, 2018 at 06:41 PM
hi, thx for your response I already fixed it by using GameObject.Find ("A").transform.localScale = new Vector3 (0, 0, 0);