Hello everyone in the community! i just would like to ask if theres a problem in instance accessing in c#. I have created several c# scripts: the first is the main where there is the oncollider script on it and some ongui functions which primary purpose is to make a quiz game.
The scripts are working great until i created 100’s of it (one question = one script). So i arranged my scripts into folders(group by subject of quiz).
To make it work i drag 7 scripts into a box where they see each other, to make the instance thing work and it worked. But later on i decided to add a sound each time the player clicks on the correct answer and i don’t want to hugely edit each script by adding variables and if statements for audio playing. So i decided create one c# script with a var of boolean value and if that boolean value is true it will play a sound.
The sound script to one of the boxes and edit the other quiz scripts and i added a line like this QuizeSound.Instance.Sounder=true; so everytime i clicked on the right answer in the quiz i will hear a sound. Working on the first box as i tested it i attached it to other quiz boxes so it will work too. Unfortunately after editing the scripts, it doesn’t appear to read the variable of the sound script. I run the game and monitored the boolean variable in runtime and its always false like theres no connection at all even the script is attached and theres a QuizeSound.Instance.Sounder=true in each script.
Is this a bug or is this normal that you can only use instance in one object because when i created another sound script instance(just renamed the class and the instance) it worked.
To clarify it more it looks like this:
Box 1 scripts: quiz1, quiz2, quiz3, quiz4, quiz5, sounder
Box2 2 sciprts: quiza, quiz2a, quiz3a, quiz4a, quiz5a, sounder
The box 1 works but the box 2 doesn’t.
Then i do this:
Box 1 scripts: quiz1, quiz2, quiz3, quiz4, quiz5, sounder
Box2 2 sciprts: quiza, quiz2a, quiz3a, quiz4a, quiz5a, sounder2
and it worked. I don’t know whats wrong in solution 1 but theres no compiling error there.
using UnityEngine;
using System.Collections;
public class QuizeSound : MonoBehaviour {
public static QuizeSound Instance;
public AudioClip impact;
public bool Sounder =false;
public int answered;
void Start ()
{
Instance = this;
}
void Update ()
{
if (Sounder == true)
{
audio.PlayOneShot(impact);
Sounder= false;
}
}
}
Above is the sounder instance code and below is a quiz script sample that is accessing it:
using UnityEngine;
using System.Collections;
public class Quize73D2 : MonoBehaviour {
public static Quize73D2 Instance;
public int activeDistance = 10;
public Transform Player;
public GUISkin customSkin;
public static int answered = 0;
void Start()
{
Instance = this;
}
void OnGUI () {
GUI.skin = customSkin;
if (Vector3.Distance(transform.position, Player.position) <= activeDistance)
{
GUI.Box(new Rect(350, 15, 800, 500), "What is the 3ds Max hotkey for rotate button?");
// Make the first button.
if (GUI.Button(new Rect(500, 120, 500, 30), "R"))
{
QuizeController3D2.Instance.ShowQuize73D2 = false;
QuizeController3D2.Instance.WonOrFailed3D2 = true;
}
// Make the second button.
if (GUI.Button(new Rect(500, 200, 500, 30), "E"))
{
QuizeController3D2.Instance.answered++;QuizeSound.Instance.Sounder=true;
QuizeController3D2.Instance.ShowQuize73D2 = false;
QuizeController3D2.Instance.WonOrFailed3D2 = true;
//One wrong answer means fail and this is a wrong answer therefore display a "you are failed" message then exit
}
// Make the 3rd button.
if (GUI.Button(new Rect(500, 280, 500, 30), "C"))
{
QuizeController3D2.Instance.ShowQuize73D2 = false;
QuizeController3D2.Instance.WonOrFailed3D2 = true;
//One wrong answer means fail and this is a wrong answer therefore display a "you are failed" message then exit
}
// Make the 4th button.
if (GUI.Button(new Rect(500, 360, 500, 30), "Alt + shift"))
{
QuizeController3D2.Instance.ShowQuize73D2 = false;
QuizeController3D2.Instance.WonOrFailed3D2 = true;
//One wrong answer means fail and this is a wrong answer therefore display a "you are failed" message then exit
}
}
}
}