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 ekim611 · Nov 03, 2011 at 07:12 PM · collisionobjecthideshow

on collision, show/hide other model scripting ?

I have a feeling this should be relatively easy, but im new to scripting so please forgive my ignorance.

I'm trying to get a model ive placed in the scene to be hidden, but show itself when a collision with another object is detected.

This is my script, its attached to the collision object :

// initially hides the model
var trigVis : boolean = false;
//on collision with object. doesnt need to disappear on trigger exit
function OnTriggerEnter(other : Collider) //attached prefab
{
    trigVis = true;
}
//call every frame
function Update()
{
    if (trigVis == true)
    {
// im not sure this part is right?    
        transform.Find("gameModel").renderer.enabled = true;
    }
    if (trigVis == false)
    {
            transform.Find("charDoneComplt").renderer.enabled = false;
    }
}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Yoerick · Nov 03, 2011 at 08:24 PM

Hi,

First of all, you want to avoid using the Find function inside the Update function. The reason behind this is that it's quite expensive to search your entire scene for a specific object. If you place this inside the Update function, Unity will scan your entire scene for the object, every single frame, making your game way slower. To solve this, create two (gameobject) properties at the beginning of your script (like your trigVis boolean) and simply initiate the two objects in a Start function like so:

var gameModel:GameObject; var charDoneComplt:GameObject;

function Start() { gameModel = transform.Find("gameModel"); transform.Find("charDoneComplt"); }

Now you can use these two objects in the OnTriggerEnter function like so:

function OnTriggerEnter()
{
    if(trigVis)
    {
        gameModel.renderer.enabled = true;
        charDoneComplt.renderer.enabled = false;
    }
}

Btw, what do these two object actually represent? because you say you want to make an object visible when it collides but in your bit of code you turn off another object? (with renderer.enabled = false)

If you only want to make the colliding object visible you should try attaching this to it:

function Start() { gameobject.renderer.enabled = false; }

function OnTriggerEnter(col:Collider) { gameobject.renderer.enabled = true; }

simple as that :) if you want to do something with the other object (which it's colliding with) you can use col.gameobject.renderer.enabled inside the OnTriggerEnter function.

If you want to, it can be even easier. If you want the object to be invisible by default, you don't actually have to disable the object's renderer in code. If you select the object in Unity there should be a "Renderer" component attached to it, which you can simply turn off by unchecking the checkbox next to it. Then these 4 lines of code will make it visible when it collides:

function OnTriggerEnter(col:Collider)
{
    gameobject.renderer.enabled = true;
}

I hope this long answer doesn't confuse you too much, just wanted to give you the reasoning behind the code ;) The only thing you need about this answer is the 4 lines of code at the end ;)

Hope it helps,

Yoerick

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 aldonaletto · Nov 03, 2011 at 09:52 PM 0
Share

@ekim611, transform.Find only searches in the object's children. If game$$anonymous$$odel and chrDoneComplt aren't children of this object, you must use GameObject.Find ins$$anonymous$$d.

@Yoerick, you forgot to assign the second object:

      charDoneComplt = transform.Find("charDoneComplt");
avatar image Yoerick · Nov 03, 2011 at 09:55 PM 0
Share

apparently I read over it a little too quickly. transform.Find indeed only looks for the object inside the specified transform. It's GameObject.Find that searches everything, my bad ;)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Disable and hide object after collision between player and cube 1 Answer

Object Visibility 2 Answers

Objects Appearing 1 Answer

3D model not showing properly at runtime, 0 Answers

Re-hiding a guiTexture 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