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 Kenshen112 · Jul 02, 2013 at 02:16 AM · newbiequest

A quick question C#

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 () {
         
         }
     }
     
Comment
Add comment · Show 7
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
avatar image cdrandin · Jul 02, 2013 at 02:49 AM 0
Share

define already your different variables and just add onto them and when you are done just nullify them till you need them late again.

avatar image ebogguess · Jul 02, 2013 at 02:53 AM 0
Share

$$anonymous$$ore information about your goals would be helpful. For instance, what information do you want access to when questing? When you say "lots of variables" it makes me think that you might want to define a class for a quest in progress that has some success condition and some variables to track progress.

avatar image Kenshen112 · Jul 02, 2013 at 05:26 AM 0
Share

^ exactly what I am trying to achieve!

avatar image Em3rgency · Jul 02, 2013 at 06:15 AM 0
Share

This is unrelated to your question, but just a general coding tip.

 if(Questing) //is the same as if(Questing == true)
 if(!Questing)//is the same as if(Questing == false)
avatar image Kenshen112 · Jul 02, 2013 at 02:34 PM 0
Share

^ Thanks but Now back to the question any one?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Em3rgency · Jul 02, 2013 at 03:07 PM

Ok I understand your problem now. You should read up on variable scope http://www.codecandle.com/articles/csharp/variables/variable-scope.html

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.

Comment
Add comment · Show 3 · Share
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
avatar image Kenshen112 · Jul 02, 2013 at 03:20 PM 0
Share

Oh ok that makes so much sense... I get it so ins$$anonymous$$d of having one
variable bool questing that loads a giant pile of variable it's better practice and actually works to have separate class variable that I can load in separate pieces. So if I decide to have a timer I just load the timer class if i want questing to be true i just load the questing class...

avatar image Em3rgency · Jul 02, 2013 at 03:28 PM 0
Share

Wait... what? They're in the same class. The good practice is making a FUNCTION to get/set them ins$$anonymous$$d of doing it directly. Questing and timer are still variables, not classes. Quests is the only class here.

I think you misread my answer. The 2 codes I posted, are the same code. First one is what you wanted, without anything extra. The second one is how it SHOULD be done, with the extra functions.

avatar image Kenshen112 · Jul 02, 2013 at 03:29 PM 0
Share

^ yeah sorry i meant that :P

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Help on a basic script 2 Answers

Complete beginner. Where to start? 0 Answers

Missile launcher is malfunctioning 2 Answers

UnityEngine.Input.GetMouseButton(1)) issue 1 Answer


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