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 scutamil · Mar 07, 2013 at 12:57 PM · colliderupdateraycasthit

I need to call something only once in Update(). (??)

Hey Guys ,

I'm very new at coding so this could be something very basic but I hope you can help me out!

So my problem is that I have a Collider and a Raycaster and I want to get information from the collided object, which I managed to get but I can't "store" it because it only contains the information I'm looking for, for one call after that it gets overwritten in the Update().

I paste my test Code here maybe you could understand from it better what I wish to achieve

 void Update()
     {
         // True if something is hit
         somethingishit = Physics.Raycast(ray,out hit,distance);
         //This works fine
         ray = mainCam.ViewportPointToRay(new Vector3 (0.5f,0.5f,0));        
         
         Debug.DrawRay(ray.origin,ray.direction*distance,Color.yellow);
         
         
         if(somethingishit)
         {        
             Debug.Log(initialColor.ToString(teszt));
             
             
             // Store the object that was hit
             previousObj = hit.collider.transform.gameObject;
             Debug.Log(previousObj.name);
             
             // The hitted object's original color
             initialColor = previousObj.renderer.material.color;
             
             // Make the hitted object colored red
             // So everything works fine until here because when Update() is called again
             // the initialColor will be red aswell, so in the next if statement it wont
             // change a thing.
             hit.collider.renderer.material.color = selectionColor;
             
                 
         }
         
         // if nothing is being hit right now but something was hitted before
         if (!somethingishit && previousObj != null)
         {
             //this should set the previously hitted object's color back to its original,when nothing is hit
             previousObj.renderer.material.color = initialColor;    
             
         }        
             
     }

Thank you for your time. Robert

Comment
Add comment · Show 1
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 rpmlins · Mar 07, 2013 at 01:48 PM 0
Share

Have you tried to debug your code in $$anonymous$$ono, using break points and line by line and see what is really hapenning?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by coffiarts · Mar 07, 2013 at 01:51 PM

First of all, you should be aware that - as your Update() is called every single frame, - it is most likely that the Ray will return the same target repeatedly (I don't assume that your camera changes orientation with every frame). This means that the following will happen:

In frame 1, the lines

 previousObj = hit.collider.transform.gameObject;
 initialColor = previousObj.renderer.material.color;

will assign the initialColor as intended (let's assume the original color is BLUE)

Then, further below, the line

 hit.collider.renderer.material.color = selectionColor;

will assign your new color (RED) to the target object (as intended).

So far, so good. At this point in time, initialColor contains BLUE, and the object is RED (as intended).

Then, however, in frame 2, the first two lines will set initialColor to RED again, because the target hit (previousObj) is still the same object, which now actually is RED. Consequently, the second code part resets the same object's color back to RED. At that point, both initialColor and the object are RED (you've got yourself caught in a RED trap :)).

So what's the solution?

As I assume that you need the initialColor (e.g. for restoring the various targets' colors later), you should find a way to store initialColor for each object separately, e.g. in an array. An easy (albeit not very elegant) approach would be to maintain two different arrays (with numeric indexes), one storing the hit objects, and one storing their initial colors. Something like...

 private GameObject[] hitTargets;
 private Color[] initialColors;

You then have to ensure two things:

  1. That for every hit target, both the object and its color are stored in these arrays, with the same(!) index. A nicer solution would be to use a multidimensional array, but I am not too familiar with it in Unity scripting yet

  2. That BEFORE changing an object's color, you have to check whether it has already been hit (by checking against the array)

I cannot go much more into the details right now. But you should spend some thoughts on getting familiar with arrays (if not already done).

Maybe my solution is not very state-of-the-art, but I hope that I could at least explain the cause of the problem!

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

12 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

Related Questions

Physics.Raycast not hitting anything 1 Answer

Raycast goes through collider 2 Answers

Player Attacking Enemy Issue 0 Answers

Multiple hit detection with Raycasting? 2 Answers

Raycast is returnig hits only once per object? 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