So I’m trying to program a questing system… And i have a question i want to program a quest definition file in the sense i can enable the bool when i’m programming a quest and i suddenly have access to several different variables… How do i do this in C#? I already have a quest code going i just need help
using UnityEngine;
using System.Collections;
public class Quests : MonoBehaviour {
public static bool Questing;
// Update is called once per frame
public static void OnGui () {
// Is Questing Allowed?
if(Questing == false){
GUI.Box(new Rect(10,10,10,10),"No Quests!!!");
}
if (Questing == true){
float timer;
Money.enabled = true;
}
}
void rewards () {
}
}
Ok I understand your problem now. You should read up on variable scope CodeCandle.com is for sale | HugeDomains
You set up the questing bool as a public variable inside your class.
While you set up float timer as a variable inside a method(function) of your class.
Nothing outside a class can see whats inside a classes functions, they can only see whats set to public in the class itself. So, to access the timer, change your code to this
using UnityEngine;
using System.Collections;
public class Quests : MonoBehaviour
{
public static bool Questing;
public float timer;
public static void OnGui ()
{
// Is Questing Allowed?
if(Questing == false)
{
GUI.Box(new Rect(10,10,10,10),"No Quests!!!");
}
if (Questing == true)
{
Money.enabled = true;
}
}
void rewards ()
{
}
}
However, this is considered bad programming practice. Ideally, you want to make all the variables private and create get/set public methods for each one.
using UnityEngine;
using System.Collections;
public class Quests : MonoBehaviour
{
private bool Questing;
private float timer;
void OnGui ()
{
//Shortened code
}
void rewards ()
{
}
public bool getQuesting()
{
return Questing;
}
public float getTimer()
{
return timer;
}
public void setQuesting(bool newValue)
{
Questing = newValue;
}
public void setTimer(float newValue)
{
timer = newValue;
}
}
Now the only way to read/change the private classes variables are by using special public methods. This ensures that you never make any mistakes, and is just considered proper coding.