I am a newbie to unity so i need your help on this simple script gets the total of everything in the array quizzes then divides it by 5 to find the average. I keep getting the error: quiz1 does not exit in the current context same thing for quiz2,quiz3,quiz4 and quiz5.
using UnityEngine;
using System.Collections;
public class myScript : MonoBehaviour {
private float total;
public float[]quizzes={quiz1,quiz2,quiz3,quiz4,quiz5};
public int average;
void Start () {
for(i=0,i<quizzes.length,i++)
{
quizzes=Random.Range(0f,10.1f);
total+=quizzes;
}
average=total/5;
Debug.Log(average);
}
// Update is called once per frame
void Update () {
}
}
You’re creating an array of quizzes. But what are quiz1 through quiz5? They’re undeclared names, the compiler has no idea what to do with them.
An array should be initialized with the values in it.
public float[] quizzes = new float[] {1f, 6f, 2f, 0.5f, 8.1f};
I also notice later in your script you say:
quizzes = Random.Range(0f, 10.1f);
quizzes is an array, Random.Range returns a float… this is going to through an error as well.
Same goes with this:
total += quizzes;
You’re mixing up your types a lot here.
Also why is ‘total’ declared as a class level field? Will that really be needed through out the life of the object? Or is it really just used for determining the average. If it’s just to determine the average… only declare it in the function that determines said average, instead of wasting memory over the life time of the object.
You probably mean to say:
using UnityEngine;
using System.Collections;
public class myScript : MonoBehaviour
{
public float[] quizzes;
public int average;
void Start ()
{
quizzes = new float[5];
var total = 0f;
for(i=0; i < quizzes.Length; i++)
{
quizzes[i] = Random.Range(0f,10.1f);
total += quizzes[i];
}
average = total / quizzes.Length;
Debug.Log(average);
}
}
I DONT GET THIS CLASS LEVEL SUPER ADVANCED STUFF I JUST MAKE A FREAKING VARIABLE I DONT KNOW ABOUT SOME STUPID MEMORY THAT IS GOING ON. SO IF YOU CAN KINDLY EXPLAIN THINGS INSTEAD OF USING THESE SUPER ADVANCED TECHY WORDS I WOULD NOT UNDERSTAND.
You defined an array of floats called ‘quizzes’, and then you’re attempting to set the entries of the array to quiz1 thru quiz5. WHAT ARE quiz1 thru quiz5? You never declared them anywhere.
Now… if we say words you don’t know. There’s this magic little thing called ‘the internets’ with a website called ‘google’.
These concepts are readily available across the internet.
Microsoft (the developers of C# as a language) have a HUGE source of knowledge that you can get a lot of it from. It’s called MSDN:
You might want to take a step back and learn the fundamentals of the language and paradigm you’re attempting to program in.
Or… more concisely… I got a tattoo years back for people who get snippy like you:
I really suggest you go and read that linked MSDN source. The fact you don’t know how to declare an array, or that a for loop uses semi-colons, not commas. Or that you don’t know what a class is… I can’t spend the time defining all that stuff for you.
It says my Global Ammo Does not exist in my current context any solution?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunFire : MonoBehaviour
{
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
AudioSource gunsound = GetComponent<AudioSource>();
gunsound.Play();
GetComponent<Animation>().Play("GunShot");
GlobalAmmo.CurrentAmmo -= 1;
}
}
}