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 Nanou · Sep 07, 2013 at 07:57 PM · c#raycastselecting

C# Raycast Prefab script activation

Hai.

I am not entirely sure how to explain it so that anyone but myself will understand it, but I'll give it my best.

I am making a 3D Minesweeper kind of game. I spawn in a cube that consists of multiple smaller cubes and you need to find the bombs in all of those. Now I need to be able to select one of those cubes to be able to get rid of them or plant a flag. So I've got this prefab Cube, which I spawn in multiple times, to build the big cube. I do that with this script:

 public class StartGame : MonoBehaviour {
     
     public static int size = 20;
     
     // Use this for initialization
     void Start () {
         //spawn light
         Instantiate(Resources.Load("Directional light"));
         //spawn cube       
         for (int i = 0; i < size; i++) 
         {
             for (int j = 0; j < size; j++) 
             {
                 for (int k = 0; k < size; k++)
                 {
                     Instantiate(Resources.Load("Cube"), new Vector3(i * 1.0F, j * 1.0F, k * 1.0F), Quaternion.identity);
                 }
             }        
         }
         
         //spawn player
         Instantiate(Resources.Load("Player/FirstPersonController"), new Vector3(size / 2, size + 1, size / 2), Quaternion.identity);        
     }
     
 
     
     // Update is called once per frame
     void Update () {
     
     }
 }

That works perfectly. But, after this is done, and the player has chosen a cube to get rid of, I want them to be able to select that cube by pointing/looking at it and actually SEE it selected, for example it changes texture or something. I thought that doing so with a Raycast would be a good way. And this is how I thought it would work:

     //assign shader with raycast
     Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
     RaycastHit hit;
     if (Physics.Raycast(ray, out hit, 5.0F))
     {
          if (hit.collider.gameObject.tag == "Cube")
          {
               Debug.Log ("i change color");
               Material selectedshader = Resources.Load("blue", typeof(Material)) as Material;
               renderer.renderer.material = selectedshader;
          }                   
      }

I've put the script with this code on the prefab, so that every cube would have it. But now when I try to select one of them, ALL of them get changed into blue (the material they change to when they are selected). So that just kills the point of selecting one of them.

Does anybody know how to fix this? Or if you know a better way of doing it, please let me know :)

Note: I am still fairly new at C#, so please be gentle. ;)

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
Best Answer

Answer by PuneetK · Sep 07, 2013 at 10:49 PM

Because when you change the Material, the material is changed for all objects which have that material. Instead, change the Texture of the gameobject (cube) clicked.

 ObjectClicked.renderer.material.mainTexture = selectedshader;
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 Nanou · Sep 08, 2013 at 10:02 AM 0
Share

Hm, it didn't work entirely as you said it, but after looking around a bit I got it to work :D Thank you :)

avatar image PuneetK · Sep 08, 2013 at 08:03 PM 0
Share

Yeah obviously you'd have to change a few other things, but I gave you the main point. Vote best please

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

16 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

Related Questions

Select an Enemy C# 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Bottom area of a 3D model? Raycast? 1 Answer

How to get the distance between two objects in feet/meter? 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