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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by PippyLongbeard · Nov 28, 2013 at 05:15 AM · dialogue

Make a function give different outputs depending on amount of left-clicks.

So, my question is if it's possible for a function of a class I made to give a different output depending on the amount of left mouse clicks it gets. Specifically, I'm trying to make a dialogue that changes when the character clicks his mouse, and gets the dialogue sentences from the inspector. The script I've got so far allows the player character to click on an NPC and get a sentence. However, I can't figure out how to change the sentence he gets, simply because I keep having to run the function of my class every time the player clicks the mouse again, resetting all the variables and such. I get an error every time I try to use variables declared at the beginning of the script to store the sentences. If someone could point out how I might accomplish this, I'd be very appreciative. Code: using UnityEngine; using System.Collections;

 public class Dialogue : MonoBehaviour {
     
     public string sentence1;
     public string sentence2;
     RaycastHit rayHit;
     string greetString;
     bool runGUI = false;
     bool GUIactive = false;
     public GameObject player;
     public GUISkin guiSkin;
     bool fin1 = false;
     bool fin2 = false;
     
     public class DialogueClass
     {    
         public string getDialogue(RaycastHit rayTarget, string sen1, string sen2)
         {
             
             string conversant = rayTarget.collider.name;
             if (conversant == "Man1")
             {
                 return sen1;
                 
             }
             if (conversant == "Man2")
             {
                 return sen2;
             }
             else
             {
                 return "";
             }
             
         
         }            
     }
     
     DialogueClass TestDialogue = new DialogueClass();
     
     void Start () {
     }
     
     void Update () {
         
         Vector3 fwd = transform.TransformDirection (Vector3.forward);    
         if ((Physics.Raycast (transform.position, fwd, out rayHit, 5)))
         {
             if (Input.GetKeyDown ("mouse 0"))
             {
                 greetString = TestDialogue.getDialogue (rayHit, sentence1, sentence2);
                 GUIactive = true;
                     
             
                 if (GUIactive)
                 {
                     runGUI = true;
                 }
             }
             
         }
         else
         {
             runGUI = false;
             GUIactive = false;
         }
         
     }
     void OnGUI () {
         
         GUI.skin = guiSkin;
         
         if (runGUI)
         {
             GUI.Box (new Rect(0,Screen.height * 0.8f,Screen.width,Screen.height * 0.2f), greetString);
         }
         
     }    
 }

By the way, the 'GUIactive' boolean is there because if I don't use it, the 'Input.GetKeyDown' function causes the dialogue to flicker there for a fraction of a second before disappearing. Very unclean, but I can't figure out another way to do it. Thanks in advance.

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

1 Reply

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

Answer by CoalCzar · Nov 28, 2013 at 05:28 AM

If I understand you correctly, I would declare a variable in your Dialogue class to hold the number of clicks and then pass it as a parameter to your DialogueClass function.

 // declare this with your other Dialogue : MonoBehaviour class variables
 
 private int numOfClicks;
 
 // add a parameter for numOfClicks in your getDialogue function
 
     public class DialogueClass
     {  
        public string getDialogue(RaycastHit rayTarget, string sen1, string sen2, int clicks)
        {
  
          string conversant = rayTarget.collider.name;
          if (conversant == "Man1")
          {
           //Check for the clicks here
           if(clicks == 1)
           return sen1;
           else
           // return something else or check for a different number of clicks
  
          }
          if (conversant == "Man2")
          {
           return sen2;
          }
          else
          {
           return "";
          }
        }        
     }
 
 // Add to numOfClicks in your Input check for mouse clicks.
 
     void Update () {
  
        Vector3 fwd = transform.TransformDirection (Vector3.forward); 
        if ((Physics.Raycast (transform.position, fwd, out rayHit, 5)))
        {
          if (Input.GetKeyDown ("mouse 0"))
          {
 // increment numOfClicks
 numOfClicks++;
 
 // pass numOfClicks as an argument
           greetString = TestDialogue.getDialogue (rayHit, sentence1, sentence2, numOfClicks);
           GUIactive = true;
 
  
  
           if (GUIactive)
           {
               runGUI = true;
           }
          }
  
        }
        else
        {
          runGUI = false;
          GUIactive = false;
        }
  
     }
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 PippyLongbeard · Nov 29, 2013 at 02:20 AM 0
Share

Ah, thank you. For some reason, I was thinking that the numOfClicks variable would reset every time I called the getDialogue() function from Update(). Anyways, thanks!

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Got a problem with script 4 dialogues 0 Answers

Auto-type text does not change to the next sentence 1 Answer

How to make dialogue in Unity3D? 2 Answers

Execute String as Code Line? 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