I found a problem with the Array in C#. I believe the code below is correct. But it keeps giving me an error
Below is just some component C# codes translated from Javascript.
With this code you are able to change the texture of an object with a click.
Could any one help me solve this problem please…
using UnityEngine;
using System.Collections;
using System;
public class Link: MonoBehaviour{
public Texture diffuseTexture;//Textures 0
public Texture diffuseTexture1;//Textures 1
public Texture diffuseTexture2;//Textures 2
//Array Size
public int intTexture = 3;
//Counter
public int i = 0;
//Declaring Texture array
Texture[]textureArray;
textureArray = new Texture[intTexture];
textureArray[0] = diffuseTexture;
textureArray[1] = diffuseTexture1;
textureArray[2] = diffuseTexture2;
public void DoAction () {
renderer.material.SetTexture("_MainTex", textureArray[i]);
i++;
if(i>=3){
i=0;
}
}
}
i don’t understand much of C# but there seems to be no blank between Texture[ ] and textureArray. just a guess though, if that’s not the error, i’m the wrong person to ask
using UnityEngine;
using System.Collections;
using System;
public class SwitchImage: MonoBehaviour{
public Texture diffuseTexture;//Textures 0
public Texture diffuseTexture1;//Textures 1
public Texture diffuseTexture2;//Textures 2
public int intTexture = 3;
//Counter
public int i = 0;
//Declaring Texture array
Texture[] textureArray = new Texture[intTexture];
textureArray[0] = diffuseTexture;
textureArray[1] = diffuseTexture1;
textureArray[2] = diffuseTexture2;
public void DoAction () {
renderer.material.SetTexture("_MainTex", textureArray[i]);
i++;
if(i>=3){
i=0;
}
}
}
Now I commented the old ARRAY.
Now I am using the ARRAY provided by Unity API…T_T
Still no LUCK…
using UnityEngine;
using System.Collections;
using System;
public class SwitchImage: MonoBehaviour{
public Texture diffuseTexture0;//Textures 0
public Texture diffuseTexture1;//Textures 1
public Texture diffuseTexture2;//Textures 2
public int intTexture = 3;
//Counter
public int i = 0;
//Declaring Texture array
/*
Texture[] textureArray = new Texture[intTexture];
textureArray[0] = diffuseTexture;
textureArray[1] = diffuseTexture1;
textureArray[2] = diffuseTexture2;
*/
/*
Texture[] myTexture;
myTexture = new Texture[diffuseTexture];
*/
Texture textureArray = new Array();
textureArray.Push(diffuseTexture0);
textureArray.Push(diffuseTexture1);
textureArray.Push(diffuseTexture2);
public void DoAction () {
renderer.material.SetTexture("_MainTex", textureArray[i]);
i++;
if(i>=3){
i=0;
}
}
}
It doesn’t matter what IDE you use - ultimately it’s Unity that’s processing your script.
You can’t work with an array - or basically anything - outside a function. You should fill your array in a function - for example the Awake() function.
Whenever you see such a parsing error, just double-click it and Unity will open the script file and point you at the violating line. The error itself also contains the line and character number. Note that Visual C# usually gives you more comprehendable error messages. In this case, it’ll say something like ‘Array size cannot be specified in a variable declaration (try initializing with a new expression)’.
using UnityEngine;
using System.Collections;
using System;
public class SwitchImage: MonoBehaviour{
public Texture diffuseTexture0;//Textures 0
public Texture diffuseTexture1;//Textures 1
public Texture diffuseTexture2;//Textures 2
public int intTexture = 3;
//Declaring textureArray and Size
Texture[] textureArray = new Texture[intTexture];
//Counter
public int i = 0;
public void DoAction () {
textureArray[0] = diffuseTexture;
textureArray[1] = diffuseTexture1;
textureArray[2] = diffuseTexture2;
renderer.material.SetTexture("_MainTex", textureArray[i]);
i++;
if(i>=3){
i=0;
}
}
}
You can’t use a member variable like intTexture to initialize your array there. It’s not accessible at that point. Allocate your array in a function, or use a static variable (that will limit all your instances to have the same number of textures though).
In C#, you would normally perform such initialization in a constructor. For objects derived from MonoBehaviour, Awake() is a good point to perform initialization.
using UnityEngine;
using System.Collections;
using System;
public class SwitchImage: MonoBehaviour{
public Texture diffuseTexture0;//Textures 0
public Texture diffuseTexture1;//Textures 1
public Texture diffuseTexture2;//Textures 2
static int textureSize = 3;
void Awake()
{
Texture[] textureArray = new Texture[3];
textureArray[0] = diffuseTexture;
textureArray[1] = diffuseTexture1;
textureArray[2] = diffuseTexture2;
doAction(textureArray);
}
//Counter
public int i = 0;
public void DoAction (Texture[] array) {
renderer.material.SetTexture("_MainTex", array[i]);
i++;
if(i>=textureSize){
i=0;
}
}
}
Could you help me fix the Code please. So I have give the best effort. I really appreciate your time Pieter.