- Home /
Having a problem with the second material (on the same renderer)
Hello, I am working on a character creation and I encountered a problem on my eye system. So I am using two materials (iris and pupil) on the same renderer, but when I try to access the second by script, I get a range error. Here's what the code looks like :
public int pupil;
public void SetPupil (int pupil) {
if (PhotonNetwork.connected)
gameObject.GetPhotonView ().RPC ("SetPupil", PhotonTargets.AllBuffered, (float)pupil);
else
SetPupil ((float)pupil);
}
[PunRPC] void SetPupil (float pupil) {
this.pupil = (int)pupil;
Texture texture = Resources.Load ("Customization/Pupil/" + pupil) as Texture;
SearchForEyes ();
rightEye.Find ("Eye Outer").GetComponent<Renderer> ().materials[1].mainTexture = texture;
leftEye.Find ("Eye Outer").GetComponent<Renderer> ().materials[1].mainTexture = texture;
}
public Color pupilColor;
public void SetPupilColor (Color pupilColor) {
if (PhotonNetwork.connected)
gameObject.GetPhotonView().RPC("SetPupilColor", PhotonTargets.AllBuffered, pupilColor.r, pupilColor.g, pupilColor.b);
else
SetPupilColor (pupilColor.r, pupilColor.g, pupilColor.b);
}
[PunRPC] void SetPupilColor (float r, float g, float b) {
this.pupilColor = new Color(r, g, b);
SearchForEyes ();
rightEye.GetChild(1).GetComponentInChildren<Renderer> ().materials[1].color = this.pupilColor;
leftEye.GetChild(1).GetComponentInChildren<Renderer> ().materials[1].color = this.pupilColor;
}
public int iris;
public void SetIris (int iris) {
if (PhotonNetwork.connected)
gameObject.GetPhotonView ().RPC ("SetIris", PhotonTargets.AllBuffered, (float)iris);
else
SetIris ((float)iris);
}
[PunRPC] void SetIris (float iris) {
this.iris = (int)iris;
Texture texture = Resources.Load ("Customization/Iris/" + iris) as Texture;
SearchForEyes ();
rightEye.Find ("Eye Outer").GetComponent<Renderer> ().materials[0].mainTexture = texture;
leftEye.Find ("Eye Outer").GetComponent<Renderer> ().materials[0].mainTexture = texture;
}
public Color irisColor;
public void SetIrisColor (Color irisColor) {
if (PhotonNetwork.connected)
gameObject.GetPhotonView().RPC("SetIrisColor", PhotonTargets.AllBuffered, irisColor.r, irisColor.g, irisColor.b);
else
SetIrisColor (irisColor.r, irisColor.g, irisColor.b);
}
[PunRPC] void SetIrisColor (float r, float g, float b) {
this.irisColor = new Color(r, g, b);
SearchForEyes ();
rightEye.GetChild(1).GetComponentInChildren<Renderer> ().materials[0].color = this.irisColor;
leftEye.GetChild(1).GetComponentInChildren<Renderer> ().materials[0].color = this.irisColor;
}
As you can see the code is quite long, but the error is in these four lines :
rightEye.GetChild(1).GetComponent<Renderer> ().materials[1].mainTexture = texture;
leftEye.GetChild(1).GetComponent<Renderer> ().materials[1].mainTexture = texture;
rightEye.GetChild(1).GetComponent<Renderer> ().materials[1].color = this.pupilColor;
leftEye.GetChild(1).GetComponent<Renderer> ().materials[1].color = this.pupilColor;
That means it is really when try to access the second material. I made sure the second material really exist on the GameObject. How can I solve this? Thank you for your time!
Comment