CS8025: Parsing error...C# Array T_T

Good Day Unity Users.

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… :smile:

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;
		}	
	}
}

Could any one help me solve this problem please… :smile:

Thank you guys :smile:

Krik Krik Krik Krik Silent… Hello00 Hell000oooo :smile:

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

You have to replace :

Texture[]textureArray; 
textureArray = new Texture[intTexture];

by :

Texture[] textureArray = new Texture[intTexture];

(It’s works in my IDE)

Unfortunately it does not work :frowning:

Thanks for the help man :smile:

I’ll give it a shot…

It still gives me the same error T_T

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;
		}
	}
}

I wrote this and its works :

private Texture[ ] textureArray = new Texture[3];

void Test(){
textureArray[0] = diffuseTexture;
}

Maybe arays can be only affected in function, my knowledge stop here about c# arays :smile:

:smile: Thanks Man

But, which IDE do you use?

do you use the UNITY3D IDE or Microsoft IDE?

Unity IDE :wink:

Code Update…

Now I commented the old ARRAY.
Now I am using the ARRAY provided by Unity API…T_T

Still no LUCK… :frowning: :frowning:

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;
		}
	}
}

:frowning: :frowning:

Array class is only available in JavaScript :wink:

http://unity3d.com/support/documentation/ScriptReference/Array.html

hahahahaha yeppp :sweat_smile:

I was just experimenting, hoping it works :smile:

Unfortunately nope :frowning: :sweat_smile:

Sennin, thanks man…:smile:

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)’.

Hmmmmmm I do know that…so I could not set it outside a function?

I must put it inside a function provided by Unity e.g.Start(), Awake(), OnGUI(), Update etc?

Is it possible to create an array inside my custom function?

Btw thanks for the mind openning respond :smile:

Any function will do, not just MonoBehaviour functions. As long as you properly set up the array before it’s being used you’re fine.

Still does not work T_T

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.

Right… :sweat_smile:
It still does not work

Now it gives me 2 errors :smile:

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. :frowning:

Look carefully at the error message and then at your variable names. I’m sure you can figure it out. :wink: