Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 C4p3erx · May 22, 2020 at 07:53 PM · scripting problemgameobjectraycastscripting beginnercolor change

I need help with raycasts and with color of gameobject [SOLVED]

So i am creating my second game and I want this game to be finished. I am creating a 3d Game. My player is a stationary object and he is looking at a cube. So the cube is on the ground and the cube is changing colors from red to green. I want to make my player lose if he clicks on the cube while the color of the cube is red. I Have been trying for ages and I can't find a fix to this problem. I'm using raycasts because i think this will be the easiest way but i just can't get the script to work. I hope that is all the information i needed to give. Im open to all suggestions on how to do this without raycasts and with something else. Thanks.

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 shadowpuppet · May 22, 2020 at 08:23 PM 0
Share

in the "changing colors" , is a simple swap of red to green or is there a blending? If simple swap, ins$$anonymous$$d of swapping colors maybe swap identical cubes? The green cube and red cube alternating ins$$anonymous$$d of colors. Then raycast can get the name of the object and if gets the red cube name he loses

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by silvematt · May 22, 2020 at 09:06 PM

Hello. You can do a simple thing like this:

 public class CubeStatus : MonoBehaviour
 {
       public bool isGreen = false;
       
       void SwapColor(bool green)
       {
             // Change color material
             isGreen = green;
       }
 }


In this way you've saved the state of the cube.

Where you do the Raycast, add a reference for the CubeStatus component:



 public class RaycastShooter : MonoBehaviour
 {
     [SerializeField] CubeStatus status; // Assign it in the inspector
 
     public void Shoot()
     {
         RaycastHit hit;
         if (Physics.Raycast(rayOrigin, forwardVect, out hit, range))
         {
             if (hit.transform.CompareTag("TheCube"))
             {
                 if (status.isGreen)
                     // we've hit good
                 else
                     // game over               
             }
         }
     }
 }
 


Look that in this case the Cube must have the tag "TheCube", otherwise it will not pass the if condition.

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 dahiyabunty1 · May 23, 2020 at 11:22 AM

maybe you try

if(hit.gameobject.meshrender.sharedmaterial.color == color.green) { //do stuff }

Comment
Add comment · Show 1 · 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 C4p3erx · May 23, 2020 at 10:14 PM 0
Share

Script doesn't work. It gives me an error. Error CS1061 'GameObject' does not contain a definition for 'meshrender' and no accessible extension method 'meshrender' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)

avatar image
0

Answer by C4p3erx · May 23, 2020 at 11:48 AM

Thank you both for your answers here :). @silvematt @dahiyabunty1 Setting booleans is such a obvious thing to do idk why I didn't do it tho:/. I will try both solutions today and I will let you know what works for me. I have a problem with the first script tho. So on the first script when I change the color of the cube won't it stay the same color all the time? Right now Im using coroutines for the color changing on the cube beacuse I want it constantly to be changed not only one time. The second script is good thank you:)

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

318 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera switch between child back to main camera issue 2 Answers

Unity 3D: How to make the gameobject speed increase continuously on tapping quickly? 1 Answer

How to shoot with a raycast? 2 Answers

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again 1 Answer

Trying to get a VR Gun Raycast to work 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