Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BatwingTM · Sep 15, 2014 at 09:30 AM · arrayscript error

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




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges