I attempted to create a system whereby collecting an item increases the player’s fire rate using the following pairs of scripts:
using UnityEngine;
using System.Collections;
public class ShootSpeedConsumeByContact : MonoBehaviour
{
public GameObject absorb;
public float spdValue;
private GameController playerController;
void Start()
{
GameObject playerControllerObject = GameObject.FindWithTag("Player");
if (playerControllerObject != null)
{
playerController = playerControllerObject.GetComponent<GameController>();
}
if (playerControllerObject == null)
Debug.Log("Cannot find 'PlayerController' script");
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Boundary")
{
return;
}
if (other.tag == "Player" || other.tag == "Shield")
{
Instantiate(absorb, transform.position, transform.rotation);
playerController.AddShootSpd(spdValue);
Destroy(gameObject);
}
if (other.tag == "Projectile")
{
Instantiate(absorb, transform.position, transform.rotation);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
}
and
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class PlayerController : MonoBehaviour
{
public float spread;
public float speed;
public float tilt;
public Boundary boundary;
private Rigidbody rb;
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
private AudioSource audioSource;
void Start()
{
rb = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
}
void Update ()
{
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, new Vector3 (shotSpawn.position.x-spread,shotSpawn.position.y,shotSpawn.position.z), shotSpawn.rotation);
audioSource.Play();
spread = spread * -1;
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
rb.position = new Vector3
(
Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax)
);
rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
}
public void AddShootSpd(float newShootSpdValue)
{
fireRate += newShootSpdValue;
}
}
It wasn’t working and I wanted to show my friend how the game worked, so I turned off the pickup item’s script and the game ran fine. I turned the object back on and tried to fix the code, and suddenly all my scripts ceased functioning. I tried restarting Unity and this did not work, I tried reverting the scripts to what you see above and the game still would not run. I checked an unrelated script, and suddenly all my instances of Random.Range are underlined in red:
using UnityEngine;
using System.Collections;
using System;
public class GameController : MonoBehaviour {
public GameObject hazard1;
public GameObject hazard2;
public GameObject hazard3;
public GameObject boostOxy;
public GameObject boostShield;
public GameObject player;
public GameObject playerExplosion;
public GameObject shield;
public Vector3 spawnValue;
public int spawnCount;
public int spawnCountIncrement;
public int howManyBoostOxy;
public int howManyBoostShield;
public int howManyHazard1;
public int howManyHazard2;
public int howManyHazard3;
public int wavesPerLevel;
public float oxyUseTime;
public float spawnWaitMin;
public float spawnWaitMax;
public float startWait;
public float waveWait;
public GUIText scoreText;
public GUIText restartText;
public GUIText gameOverText;
internal void AddShootSpd(float spdValue)
{
throw new NotImplementedException();
}
public GUIText oxyText;
public GUIText oxyGoneText;
public GUIText waveNum;
private int spawnNumber;
private GameObject spawnedThing;
private float spawnWait;
private bool gameOver;
private bool restart;
private int score;
private int oxygen;
private int wave;
private Vector3 spawnPosition;
private Quaternion spawnRotation;
void Start()
{
score = 0;
oxygen = 180;
wave = 1;
UpdateScore();
UpdateOxy();
waveNum.text = "Wave " + wave;
StartCoroutine(SpawnWaves());
StartCoroutine(UseOxy());
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
oxyGoneText.text = "";
shield.SetActive(false);
}
void Update()
{
if (restart)
{
if (Input.GetKeyDown(KeyCode.R))
{
Application.LoadLevel (Application.loadedLevel);
}
}
}
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < spawnCount; i++)
{
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValue.x, spawnValue.x), spawnValue.y, spawnValue.z);
Quaternion spawnRotation = Quaternion.identity;
if (wave <= wavesPerLevel)
{
spawnNumber = Random.Range(1, howManyBoostOxy + howManyHazard1);
if (spawnNumber <= howManyBoostOxy)
{
spawnedThing = boostOxy;
}
if (spawnNumber > howManyBoostOxy)
{
spawnedThing = hazard1;
}
Instantiate(spawnedThing, spawnPosition, spawnRotation);
spawnWait = Random.Range(spawnWaitMin, spawnWaitMax);
yield return new WaitForSeconds(spawnWait);
}
if (wave > wavesPerLevel)
{
spawnNumber = Random.Range(1, howManyBoostOxy + howManyBoostShield + howManyHazard1 + howManyHazard2);
if (spawnNumber <= howManyBoostOxy)
{
spawnedThing = boostOxy;
}
if (spawnNumber > howManyBoostOxy && spawnNumber <= howManyBoostOxy + howManyBoostShield)
{
spawnedThing = boostShield;
}
if (spawnNumber > howManyBoostOxy + howManyBoostShield && spawnNumber <= howManyBoostOxy + howManyBoostShield + howManyHazard1)
{
spawnedThing = hazard1;
}
if (spawnNumber > howManyBoostOxy + howManyBoostShield + howManyHazard1)
{
spawnedThing = hazard2;
}
Instantiate(spawnedThing, spawnPosition, spawnRotation);
spawnWait = Random.Range(spawnWaitMin, spawnWaitMax);
yield return new WaitForSeconds(spawnWait);
}
}
wave++;
spawnCount = spawnCount + spawnCountIncrement;
if (wave > 1 && wave <= 10)
{
spawnWaitMin = spawnWaitMin - 0.1f;
spawnWaitMax = spawnWaitMax - 0.2f;
}
if (wave > 12 && spawnWaitMax > 0.5)
{
howManyBoostOxy--;
spawnWaitMax = spawnWaitMax - 0.01f;
}
if (wave > 12 && spawnWaitMax <= 0.5)
{
howManyBoostOxy--;
}
if (wave > 20 && oxyUseTime > 0.05)
{
oxyUseTime = oxyUseTime - 0.005f;
}
yield return new WaitForSeconds(waveWait);
waveNum.text = "Wave " + wave;
if (gameOver)
{
restartText.text = "Press 'R' to restart";
restart = true;
break;
}
}
}
IEnumerator UseOxy()
{
yield return new WaitForSeconds(startWait);
while (true)
{
oxygen--;
UpdateOxy();
yield return new WaitForSeconds(oxyUseTime);
if (gameOver)
{
break;
}
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
public void AddOxy(int newOxyValue)
{
oxygen += newOxyValue;
UpdateOxy();
}
public void ActivateShield()
{
shield.SetActive(true);
}
public void DeactivateShield()
{
shield.SetActive(false);
}
void UpdateOxy()
{
oxyText.text = "Oxygen: " + oxygen;
if (oxygen <= 0)
{
Instantiate(playerExplosion, player.transform.position, player.transform.rotation);
DestroyObject(player);
GameOver();
}
}
void UpdateScore()
{
scoreText.text = "Score: " + score;
}
public void GameOver()
{
gameOverText.text = "Game Over";
gameOver = true;
if (oxygen == 0)
{
oxyGoneText.text = "you ran out of air";
}
if (oxygen > 0)
{
oxyGoneText.text = "the fish got you";
}
}
}
Visual Studio suggests I convert them to either System.Random.Range or UnityEngine.Random.Range. What do I do? How do I get my scripts to compile again? Which Random Range is the right one?