• 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 T-D-M-3 · Jun 21, 2015 at 10:48 PM · scripting problemvariablescriptingbasicssave dataquest

Scan for a variable based upon an integer

so i am trying to make a quest system for a game and the problem currently at hand is that each one of the 60 quests has a save data variable. what i need to do is make it so the script checks a variable without running if statements 60 times.

 if(questvar == 1){
 status = Savedata.Quest1;
 }
 
 if(questvar == 2){
 status = Savedata.Quest2;
 }
 
 if(questvar == 3){
 status = Savedata.Quest3;
 }
 
 if(questvar == 4){
 status = Savedata.Quest4;
 }
 
 //and continues all the way to 60
 

if there is already an answer out there please point me to it, if not any help on this would be appreciated -T.D.M.3

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by pako · Jun 22, 2015 at 09:20 AM

I think what your are trying to do can be simply accomplished as follows:

  1. Each Quest script should have a public variable "Status", which is what you want to access, and has been assumed to be int in this example (but it can be anything you want). The "Status" variable replaces the QuestX variables.

  2. All Quest scripts should be held (referenced) by the current script, meaning the one that has all the if statements.

Here's the code:

 using System.Collections.Generic; //needed to use List

 //Drag-n-Drop Quests in Inspector
 //Or use GetComponent to populate the Quests List (Quests can then be made private)
 public List<Quest> Quests;  //hold all Quests in a List. 
 private int status;
 

 //Call this method when you want to get the status
 void CheckQuestStatus(int QuestNumber)
 {
     int questIndex = QuestNumber - 1; //First List index = 0
     
     if (questIndex >= 0)
     {
         this.status = Quests[questIndex].Status;
     }
     else
     {
         Debug.Log("Quest Number must be greater than 0");
     }
     
 }


Comment
Add comment · Show 1 · 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 T-D-M-3 · Jun 22, 2015 at 11:04 AM 0
Share

I forgot to mention that i work in java script so i don't Quite know how to convert this. aside from that, I think this will work. Thanks!

avatar image
0

Answer by Xarbrough · Jun 21, 2015 at 11:20 PM

What are you exactly trying to do? I think you can greatly improve your design by getting rid of the integer "questvar".

In what form is your SaveData.QuestX?

Generally you should store all of your quests in either a Dictionary, so that you can retrieve a quest by its Dictionary key (a string name) or in a List, if they are consecutive. Then you could have a method "GetNextQuest" which just takes the current quest and returns the next item in the list.

Comment
Add comment · Show 1 · 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 T-D-M-3 · Jun 22, 2015 at 01:30 AM 0
Share

i am trying to make a single script to display progress on any given Quest. There will be 60 instances of the Quest script, however i am just trying to make a lighter simpler script to compensate. the data is actually preloaded on start-up so i just need to access a variable on the load side of the save script.

i am also trying to make it so can have all 60 Quests available at once not just a series of tasks (more free-roam style)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hello can anyone help me i am new in scripting 1 Answer

How can i check and fire an event when the user look at specific object ? 0 Answers

Adding callbacks/events to an instantiated prefab 2 Answers

How do I inherit certain varibles and functions from another script. 1 Answer

Simplify this code 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges