- Home /
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. ;)
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;
Hm, it didn't work entirely as you said it, but after looking around a bit I got it to work :D Thank you :)
Yeah obviously you'd have to change a few other things, but I gave you the main point. Vote best please
Your answer
Follow this Question
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