- Home /
Creating an array of materials in a GameController script
G'Day
I have been going through the Unity Space Shooter tutorial and have decided to play around with it and extend it's scope to learn more about Unity 3D.
What I have currently done (aside from adding my own assets) is implemented a wave count to count the waves of asteroids. Each asteroid that is spawned is randomaly selected from 2 asteroid prefabs linked to the GameController script.
What I want to do is after 10 waves change the material of Background gameObject. The way i thought would be best would be to create an array of materials in the GameController script, link them via Unity's Inspector and then increment them as the game progresses.
However (on line 25) when I try to create the array of Materials I get a CS0103 error "The name 'Material' does not exist in the current context"
I have no doubt that the mistake I havew made is simple, but I'll be buggered if I can see what it is. You will note that I have commented this code, extensively, as I like to know what it is I have done, so I can refer back.
Any assitance would be appreciated. GameController.cs is linked below
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public GameObject hazard1, hazard2;
//where we will be creating our hazards, this is supplied by Unity
public Vector3 spawnValues;
//int to srore the number of hazards we encounter, accessed from Unity
public int hazardCount;
//the time we want to wait between hazards
public float spawnWait;
//how long the player will wait until hazards start appearing
public float startWait;
//how long the player will wait between hazards waves
public float waveWait;
//our GUI text for score, restart and Game Over
public GUIText scoreText;
public GUIText restartText;
public GUIText gameOverText;
public GUIText hazardCountText;
//Materials we can feed to Unity, which will have our nebula textures
public Material nebulaMaterials = new Material();
//private variables to track GameOver, Restart and score
private bool gameOver;
private bool restart;
private int score = 0;
//count the number of waves
private int waveCount = 1;
//every 10 waves, progress stages
private int stageCount = 1;
void Start ()
{
//set the boolean flags
gameOver = false;
restart = false;
//set the GUI Texts to hold nothingt
restartText.text = "";
gameOverText.text = "";
hazardCountText.text = "";
//display the inital score
UpdateScore();
//call our SpawnWaves function, as a co-routine
StartCoroutine (SpawnWaves());
}
//allow the user to press 'R' to restart the game once it is over
void Update ()
{
//triggered gameover occurs and a restart is processed
if (restart)
{
//capture the 'R' key
if (Input.GetKeyDown (KeyCode.R))
{
//reload the current scene/level
Application.LoadLevel (Application.loadedLevel);
}
}
}
//function to change the texture of the background
function changeTex(material : Material) {
object.material = material;
}
//our function to create the hazard objects
//we need to return IEnumerator to allow WaitForSecond to be run as a co-routine
//ie, to be run at the same times as other game methods
IEnumerator SpawnWaves ()
{
//an int to help us randomly pick an asteroid type
int asteroidType = 0;
//give the User some time before hazards start appearing
yield return new WaitForSeconds (startWait);
while(true)
{
//Display the Hazard Wave number
hazardCountText.text = "Wave "+ waveCount.ToString();
//wait for one second
yield return new WaitForSeconds (1);
//clear the text in the Hazard Count (hides the text)
hazardCountText.text = "";
//for loop that will spawn the hazards 'X' times where 'X' is the number of hazards provided by Unity
for(int i = 0; i < hazardCount; i++)
{
//Vector3 to position our hazard,
//we want a random location on the x axis so we will use the Random.Range function
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
//Quaternion holds a 'rotation' value. The 'identity' method returns with NO rotation
//since our object is provided with a random rotation anyway, this is fine.
Quaternion spawnRotation = Quaternion.identity;
//a random function to decide if the asteroid is of type 1 or 2 (3 is exclusive)
asteroidType = Random.Range(1, 3);
//spawning the Asteroid we randomally picked
if(asteroidType == 1)
{
//actually create our hazard1 with the above information
Instantiate (hazard1, spawnPosition, spawnRotation);
}
if(asteroidType == 2)
{
//actually create our hazard1 with the above information
Instantiate (hazard2, spawnPosition, spawnRotation);
}
//if we hit this, we have messed something up
else
{
Debug.Log("Asteroid not found");
}
//wait for the next spawn time, using WaitForSeconds as a co-routine
//so it does not simple hold up our game
yield return new WaitForSeconds (spawnWait);
}
//add one to the wave count
waveCount++;
//wait for time between waves
yield return new WaitForSeconds (waveWait);
//if the game is over then do the following
if (gameOver)
{
restartText.text = "Press 'R' for Restart";
restart = true;
// break out of the while loop
break;
}
}
}
//method to compile our score
public void AddScore (int newScoreValue)
{
//modifies score by adding newScoreValue to it's current value
score += newScoreValue;
//display the new score
UpdateScore ();
}
//the update method for the scoring system
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
//a function to run when the game is over and reset the hazard spawning
public void GameOver ()
{
gameOverText.text = "Game Over!";
gameOver = true;
}
}
Your answer

Follow this Question
Related Questions
Problem with arrays and accessing sriptable objects. 2 Answers
My script seems to work fine , but i seem to get this error when i play 2 Answers
I need advice on how to make a right click system similar to runescapes? 1 Answer
Help with multi menu closing using bool 1 Answer
Access Array From Another Script? 2 Answers