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 AW0610AUT · Jul 27, 2014 at 09:01 PM · raycastvariablealphaif-statements

Keeping track of object that is set via "if..Raycast Statement

Hello everyone,

I have a top-down 3d Scene and I am fading out the alpha of a roof when i enter the Building. Here is the code: #pragma strict var dir: Vector3; var Obj: RaycastHit;

 function Start () {
     dir = transform.TransformDirection (Vector3.up);
 }
 
 function Update () {
     var hit : RaycastHit;
     
     if(Physics.Raycast (transform.position, dir, hit, 20)){
         var Obj = hit.transform;
         Obj.renderer.material.color.a -= renderer.material.color.a * Time.deltaTime;
     }
 
 }

It works but the problem is i need to make the roof (Alpha component) visible again after i leave, but i only get the name of the object when the Raycast hits it - because after i leave the raycast gives out Null.

How could i solve this? How can i know via script which objects Alpha component i need to set back to 1.

Thanks for the help in advance. -Alexander

Comment
Add comment · Show 3
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 robertbu · Jul 27, 2014 at 09:33 PM 2
Share

You can simply make Obj a class instance variable. Declare it at the top of the file, then you can bring the alpha back up if the Raycast() fails. But this solution has a flaw. If you have more than one object that can fade, it is possible to exit one and entera new one before the first has completely faded up. If this is an issue, I'd move the fading code to the individual objects and just have the Raycast() code tell the objects to fade up or down. That way if you move on to a new object, the original object can keep fading up until alpha is 1.0.

avatar image AW0610AUT · Jul 27, 2014 at 09:47 PM 0
Share

Thanks. I was thinking about handling this in a script on each individual Object but i was hoping there was a easier way. I guess i will have to do it that way then. Thanks for the answer.

avatar image robertbu · Jul 28, 2014 at 12:54 AM 0
Share

There is almost always more solutions. For example, you could get around the problem I outlined in the previous comment by immediately bringing the alpha to 1.0 on the previous object when a new fading object is found. Or you could use a Queue to manage a list of fading objects, but I think the right thing is to put the fading code in the individual objects.

0 Replies

· Add your reply
  • Sort: 

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

23 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

Related Questions

Altering Material Alpha While Raycast is NOT Hitting 0 Answers

how to ignore transparency of image when clicking/raycasting on it in 2D/uGUI? 0 Answers

how to Stream.serialize(stringValue) 2 Answers

C# Declaring Variables Within If Statements 2 Answers

If Statement Conditional Not Working 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