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 Volumnia · Apr 14, 2013 at 04:57 AM · javascriptupdateonguiquestguiwindow

Simple quest system: from Update function to OnGUI? (javascript)

Hello all!

First time posting here :)

I've come across a simple quest system script (which can be seen here in Portuguese) to which I made some minor tweaks. However, being no programmer myself, I'm feeling way out of my league to convert the code I've got so far - which uses mostly the Update function - to an OnGUI function in order to make it more flexible for the use of the various GUI components.

The system so far uses 2 scripts that communicate to each other. Here's the first, which goes to my quest giver NPC:

 #pragma strict
 
 var questHelper : Missoes; //calls for the script Missoes.js, not the variable 
 var wizetteDistance : Vector3;
 var talk : GUIText;
 var questDesactivation : boolean;
 
 function Start () 
 {
     questDesactivation = false;
 }
 
 function Update () 
 {
     if(questDesactivation == false) 
     {
         wizetteDistance = GameObject.FindWithTag("Player").transform.position;
         wizetteDistance = wizetteDistance - transform.position;
         talk.text = "";
         if(wizetteDistance.magnitude < 2) 
         {
             talk.text = "Press Enter to accept the mission";
             if (Input.GetKeyDown(KeyCode.Return))
             {
                 questHelper = GameObject.FindWithTag("Player").AddComponent("Missoes");
                 questHelper.questText = talk;
                 questDesactivation = true;
                 
             }
         }
     }
     else if(Input.GetKeyDown(KeyCode.Escape))
     {
         questDesactivation = false;
         
     }
 }

The following script will then be added to the player:

 #pragma strict
 
 var currentObjective : int;
 var quests : String[];
 var questText : GUIText;
 var distance : float;
 var item : GameObject;
 var itemCreation : boolean;
 var itemPosition : Vector3;
 //var questName = "Retrieve the Bottle."
 
 
 function Start () 
 {
     
     currentObjective = 0;
     quests = new String[4];
     quests[0] = "Talk to the master ";
     quests[1] = "Go to the temple ";
     quests[2] = "Search for the bottle ";
     quests[3] = "Go back and deliver it to the master ";
 
 }
 
 /*
 function OnGUI () 
 {
     questWindow = GUI.Window(0, questWindow, Quest, "Quest: " + questName);
 }
 
 function Quest (windowID : int) 
 {
     GUI.DragWindow(Rect(0,0, 10000, 20));
     GUI.Label (Rect (10, 10, 300, 310), quests[currentObjective]);
     if (Input.GetKeyDown("z"))
     {
     }
 }
 */
 
 function Update () 
 {    
     questText.text = quests[currentObjective];
     if (Input.GetKeyDown("z"))
     {
         currentObjective++;
         if (currentObjective >= quests.Length)
         {
             currentObjective = 0;
         }
     }
     
     if (currentObjective == 0)
     {
         distance = Mathf.FloorToInt(Vector3.Distance(GameObject.Find("Master").transform.position, transform.position));
         questText.text += "Dis: "+ distance;
         if(distance <= 3) 
         {
             questText.text = "You must to go the temple and look for the golden vase [Press Enter to accept it].";
             if(Input.GetKeyDown(KeyCode.Return))
             {
                 currentObjective = 1;
             }
         }
     }
     if (currentObjective == 1)
     {
         distance = Mathf.FloorToInt(Vector3.Distance(GameObject.Find("MissionPoint").transform.position, transform.position));
         //distance = Mathf.FloorToInt(Vector3.Distance(GameObject.Find("MissionPoint").transform.position, transform.position / transform.lossyScale.magnitude));
         questText.text += "Dis: "+ distance;
         if(distance <= 3) 
         {
             currentObjective = 2;
             itemCreation = false;
         }
     }
     if (currentObjective == 2)
     {
         if(!itemCreation)
         {
             item = GameObject.Find("Bottle");
             if (Random.value < 0.5) 
             {
                 itemPosition = GameObject.Find("P1").transform.position;
             }
             else
             {
                 itemPosition = GameObject.Find("P2").transform.position;
             }
             item = Instantiate(item, itemPosition, item.transform.rotation);
             itemCreation = true;
         }
         else 
         {
             distance = Mathf.FloorToInt(Vector3.Distance(item.transform.position, transform.position));
             questText.text += "Dis: " + distance;
             if(distance <= 1) 
             {
                 questText.text = "Press Enter to get the bottle. ";
                 if(Input.GetKeyDown(KeyCode.Return))
                 {
                     currentObjective = 3;
                     Destroy(item);
                 }
             }
         }
     }
     if (currentObjective == 3)
     {
         distance = Mathf.FloorToInt(Vector3.Distance(GameObject.Find("Master").transform.position, transform.position));
         questText.text += "Dis: "+ distance;
         
         if(distance <= 3) 
         {
             questText.text = "Thank you for the bottle! [Press Enter to finish the quest].";
             if(Input.GetKeyDown(KeyCode.Return))
             {
                 currentObjective = 4;
                 questText.text = "";
                 Destroy(gameObject.GetComponent(Missoes));
             }
         }
     }
     if (Input.GetKeyDown(KeyCode.Escape))
     {
         questText.text = "You quit the quest! Talk to the dummie to get the quest again. ";
 yield WaitForSeconds(8); //Not working due to the constant refreshing :( 
         Destroy(gameObject.GetComponent(Missoes));
         
     }
 
 }


My biggest issue right now is the fact that for all of this to work, I need to keep the GUIText element for the communication of the 2, as far as I see it... :/ The mechanics of it are great, and fit my needs very well. I just need this all to be inside an OnGUI function rather than displayed as a GUIText.

Ideally, the visual outcome of it would be something such as seen on this tutorial, which uses somewhat the following code:

 #pragma strict
 static var activateQuest = false;
 var questWindow : Rect = Rect (((Screen.width / 2) - 100), ((Screen.height / 2) - 100), 512, 256);
 var questName = "Test";
 var questContent = "";
 var avatar = Texture;
 
 function OnGUI () 
 {
     if(activateQuest)
     {
     questWindow = GUI.DragWindow(0, questWindow, Quest, "Quest" + questName);
     }
 }
 
 function Quest (windowID : int) 
 {
     GUI.Label (Rect (10, 10, 300, 310), questContent);
     GUI.DrawTexture(Rect(a,b,c,d), avatar);
     if (GUI.Button(Rect(((questWindow.width / 2) - 50), (questWindow.height / 2) - 100), 512, 256), "Accept")
     {
         activateQuest = true;
     }
     if (GUI.Button(Rect(((questWindow.width / 2) - 30), (questWindow.height / 2) - 100), 512, 256), "Cancel")
     {
         activateQuest = false;
     }
 }
 
 function OnMouseDown () 
 {
     renderer.material.color = Color.red;
     activateQuest = true;
 }

Ugh, I hope I'm not being confusing!

This is for a game I'm creating as a graduation project in 2 weeks from now, so other than eternal gratitude, I'll also make sure to mention you in my project :)

Maaaaaaaaaaaaaaany many thanks in advance to anyone who can give me a helping hand!

  • Laura

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

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

Variables in GUI not updating/changing? (javascript) 1 Answer

Ambiguous Label error in custom "for i in Array" GUI 2 Answers

Eliminating input loss 1 Answer

GetKeyDown is fired more than once in Update() 1 Answer

OnTriggerEnter only recognised once, no matter how many times I enter ? (Solved) 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