- Home /
Raycast pink material
Hi all, i'm a beginner with Unity and I have a problem with RayCast. My objective is to make a chess board, and to change the color of the square I click on, the selection is correct (the correct square change color), but it become pink. Here is my full code (the raycast is at line 31) :
using UnityEngine;
using System.Collections;
public class Modele : MonoBehaviour {
public Material mat;
// Use this for initialization
void Start () {
GameObject caseC, caseF;
caseC = GameObject.Find("CaseC");
caseF = GameObject.Find("CaseF");
GameObject tmp = null;
for(int i=0 ; i<8 ; i++) {
for(int j=0 ; j<8 ; j++) {
if(i%2 == j%2)
tmp = (GameObject)Instantiate(caseC);
else
tmp = (GameObject)Instantiate(caseF);
tmp.transform.position = new Vector3(i,0,j);
}
}
Destroy(caseC);
Destroy(caseF);
select = null;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)){ // if left button pressed...
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)){
hit.collider.gameObject.renderer.material = mat;
}
}
}
}
http://image.noelshack.com/fichiers/2014/51/1418648844-sans-titre.jpg
As you can see in the screen, my public variable is set. I also tried to do it with the texture directly (with a Texture variable and with the material.mainTexture attribute), but it make the area white.
An other small question would be : what is the better way to create 64 squares, I don't think my method is really good...
Sorry for my English, It's not my native language, and thank you for your time.
Pink meterial is a shader issue. Try to check if your material assigned to variable 'mat' has a proper shader to it.
Have you tested the $$anonymous$$aterial that you have assigned to the "mat" var outside of your code. When something goes pink it is often the result of a shader problem on the $$anonymous$$aterial. $$anonymous$$ake sure this $$anonymous$$aterial is fully working and let us know, thanks.
The shader I use is the default Diffuse, and yes I tried the material : on my picture the light squares does use the same one. And i noticed that when I click on a square and then explore its properties in the 3D view it doesn't have any material.
EDIT : And if I does the same with the texture modification I have the same problem : the texture isn't set anymore in a new instance of my material.
Just check if your 'mat' is null before assigning it to the game object and put a debug if it is null.
Convert your comment to an answer @taxvi Then he can accept it as such :)
Answer by taxvi · Dec 15, 2014 at 01:51 PM
select an object that the script is attached to, then in the inspector scroll down to the script and see THERE if the material is assigned. your screenshot does not tell weather the mat is assigned or not
Your answer
Follow this Question
Related Questions
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
Mouse Click + Raycast + Colliders 2 Answers