- Home /
The question is answered, right answer was accepted
Cant change material texture through C# script
why wont this work? Im getting a nullreference error on the 'rend.mater....' line
public Texture2D textures;
public float speed = 2f;
public IEnumerator LoopTexture () {
for(int i = 0; i<textures.Length; i++) {
rend.material.SetTexture("_MainTex", textures[i]);
yield return new WaitForSeconds(speed);
}
}
I have also tried:
rend.material.mainTexture = textures[i];
and tried changing "_MainTex" to "_MainTexture"
nothing will work
Answer by mtdrume · Aug 15, 2015 at 08:30 PM
added []
public Texture2d[] textures;
public float speed = 2f;
added call, not sure if you had it called from somewhere else
void Start()
{
StartCoroutine ("LoopTexture");
}
public IEnumerator LoopTexture () {
for(int i = 0; i<textures.Length; i++) {
Updated this (not sure if you were referencing getcomponent
GetComponent<Renderer> ().material.SetTexture("_MainTex", textures[i]);
yield return new WaitForSeconds(speed);
}
}
Tested and working :)
ive actually already got those lines. I just compressed it to show you the line that has the problem. I basically have the exact same code and it does not work.
in your code you have Texture2d not Texture2d[], that is the biggest thing i notice because you are not calling an array.
i rewrote it here. I must have missed that. The problem was the GetComponent, nothing else.
Follow this Question
Related Questions
Code works in editor, however not in build... C# 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Yield return request never returns 0 Answers
NullReferenceException for reasons I don't understand. 1 Answer