Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

On next click event?

Discussion in 'Scripting' started by unity_6F9py3-5BalWIA, Sep 19, 2018.

  1. unity_6F9py3-5BalWIA

    unity_6F9py3-5BalWIA

    Joined:
    Aug 13, 2018
    Posts:
    3
    So here is the story. I am trying to get this script to do a when I click "a" it shows a text box. That works so far. Currently I have it set up for OnMouseExit just to clear the text since I can't get the idea I have working.

    The idea is when you click "a" text appears. When I click "b" it destroys text and then shows text for "b". So it seems to be a onnextclick kind of event where the click is tied only to when a new "b,c, etc" is clicked. This would allow for the user to navigate the screen without having the text disappear until they click something else that is clickable. Any help?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class itemtextcontrol : MonoBehaviour {
    5.     public GameObject popuptext;
    6.     public static string textstatus="off";
    7.     private GameObject lastCreated;
    8.  
    9.  
    10.     void OnMouseDown()
    11.     {
    12.         if (textstatus == "off")
    13.         {
    14.            
    15.             switch (gameObject.name){
    16.                 case "Lot 01":
    17.                     popuptext.GetComponent<TextMesh>().text = "Lot 1\n 2.81 Acres\n 122,403.6 sq. ft\n $275,408.10";
    18.                     break;
    19.                 case "Lot 02":
    20.                     popuptext.GetComponent<TextMesh>().text = "Lot 2\n 2.519 Acres\n 109,727.64 sq. ft\n $274,319.10";
    21.                     break;
    22.                 case "Lot 03":
    23.                     popuptext.GetComponent<TextMesh>().text = "Lot 3\n 1.557 Acres\n 67,822.92 sq. ft\n $169,557.30";
    24.                     break;
    25.                 case "Lot 04":
    26.                     popuptext.GetComponent<TextMesh>().text = "Lot 4\n 1.658 Acres\n 72,222.48 sq. ft\n $180,556.20";
    27.                     break;
    28.                 case "Lot 05":
    29.                     popuptext.GetComponent<TextMesh>().text = "Lot 5\n 1.705 Acres\n 74,269.8 sq. ft\n $185,674.50";
    30.                     break;
    31.                 case "Lot 06":
    32.                     popuptext.GetComponent<TextMesh>().text = "Lot 6\n 1.34 Acres\n 58,370.4 sq. ft\n $145,926.00";
    33.                     break;
    34.                 case "Lot 07":
    35.                     popuptext.GetComponent<TextMesh>().text = "Lot 7\n 1.322 Acres\n 57,586.32 sq. ft\n $143,965.80";
    36.                     break;
    37.                 case "Lot 08":
    38.                     popuptext.GetComponent<TextMesh>().text = "Lot 8\n 1.713 Acres\n 74,618.28 sq. ft\n $186,545.70";
    39.                     break;
    40.                 case "Lot 09":
    41.                     popuptext.GetComponent<TextMesh>().text = "Lot 9\n 1.159 Acres\n 50,486.04 sq. ft\n $126,215.10";
    42.                     break;
    43.                 case "Lot 10":
    44.                     popuptext.GetComponent<TextMesh>().text = "Lot 10\n 1.467 Acres\n 63,902.52 sq. ft\n $159,756.30";
    45.                     break;
    46.                 case "Lot 11":
    47.                     popuptext.GetComponent<TextMesh>().text = "Lot 11\n 1.784 Acres\n 77,711.04 sq. ft\n $194,277.60";
    48.                     break;
    49.                 case "Lot 12":
    50.                     popuptext.GetComponent<TextMesh>().text = "Lot 12\n 1.427 Acres\n 62,160.12 sq. ft\n $155,400.30";
    51.                     break;
    52.                 case "Lot 13":
    53.                     popuptext.GetComponent<TextMesh>().text = "Lot 13\n 2.169 Acres\n 94,481.64 sq. ft\n $236,204.10";
    54.                     break;
    55.                 case "Lot 14":
    56.                     popuptext.GetComponent<TextMesh>().text = "Lot 14\n 2.3 Acres\n 100,188 sq. ft\n $252,473.76";
    57.                     break;
    58.                 case "Lot 15":
    59.                     popuptext.GetComponent<TextMesh>().text = "Lot 15\n 1.918 Acres\n 83,548.08 sq. ft\n $208,870.20";
    60.                     break;
    61.                 case "Lot 16":
    62.                     popuptext.GetComponent<TextMesh>().text = "Lot 16\n 1.174 Acres\n 51,139.44 sq. ft\n $127,848.60";
    63.                     break;
    64.                 case "Lot 17":
    65.                     popuptext.GetComponent<TextMesh>().text = "Lot 17\n 1.604 Acres\n 69,870.24 sq. ft\n $174,675.60";
    66.                     break;
    67.                 case "Lot 18":
    68.                     popuptext.GetComponent<TextMesh>().text = "Lot 18\n 1.374 Acres\n 59,851.44 sq. ft\n $149,628.60";
    69.                     break;
    70.                 case "Lot 19":
    71.                     popuptext.GetComponent<TextMesh>().text = "Lot 19\n 1.532 Acres\n 66,733.92 sq. ft\n $166,834.80";
    72.                     break;
    73.                 case "Lot 20":
    74.                     popuptext.GetComponent<TextMesh>().text = "Lot 20\n 1.89 Acres\n 82,328.4 sq. ft\n $205,821.00";
    75.                     break;
    76.                 case "Lot 21":
    77.                     popuptext.GetComponent<TextMesh>().text = "Lot 21\n 2.384 Acres\n 103,847.04 sq. ft\n $259,617.60";
    78.                     break;
    79.                 case "Lot 22":
    80.                     popuptext.GetComponent<TextMesh>().text = "Lot 22\n 1.789 Acres\n 77,928.84 sq. ft\n $194,822.10";
    81.                     break;
    82.                 case "Lot 23":
    83.                     popuptext.GetComponent<TextMesh>().text = "Lot 23\n 1.654 Acres\n 72,048.24 sq. ft\n $180,120.60";
    84.                     break;
    85.                 case "Lot 24":
    86.                     popuptext.GetComponent<TextMesh>().text = "Lot 24\n 1.403 Acres\n 61,114.68 sq. ft\n $152,786.70";
    87.                     break;
    88.                 case "Lot 25":
    89.                     popuptext.GetComponent<TextMesh>().text = "Lot 25\n 2.078 Acres\n 90,517.68 sq. ft\n $226,294.20";
    90.                     break;
    91.                 case "Lot 26":
    92.                     popuptext.GetComponent<TextMesh>().text = "Lot 26\n 1.494 Acres\n 65,078.64 sq. ft\n $162,696.60";
    93.                     break;
    94.                 case "Lot 27":
    95.                     popuptext.GetComponent<TextMesh>().text = "Lot 27\n 1.783 Acres\n 77,667.48 sq. ft\n $194,168.70";
    96.                     break;
    97.                 case "Lot 28":
    98.                     popuptext.GetComponent<TextMesh>().text = "Lot 28\n 1.732 Acres\n 75,445.92 sq. ft\n $188,614.80";
    99.                     break;
    100.                 case "Lot 29":
    101.                     popuptext.GetComponent<TextMesh>().text = "Lot 29\n 1.726 Acres\n 75,184.56 sq. ft\n $187,961.40";
    102.                     break;
    103.                 case "Lot 30":
    104.                     popuptext.GetComponent<TextMesh>().text = "Lot 30\n 4.199 Acres\n 182,908.44 sq. ft\n $365,816.88";
    105.                     break;
    106.                 case "Lot 31":
    107.                     popuptext.GetComponent<TextMesh>().text = "Lot 31\n 1.456 Acres\n 63,423.36 sq. ft\n $158,558.40";
    108.                     break;
    109.                 case "Lot 32":
    110.                     popuptext.GetComponent<TextMesh>().text = "Lot 32\n 1.46 Acres\n 63,597.6 sq. ft\n $158,994.00";
    111.                     break;
    112.                 case "Lot 33":
    113.                     popuptext.GetComponent<TextMesh>().text = "Lot 33\n 1.453 Acres\n 63,292.68 sq. ft\n $158,231.70";
    114.                     break;
    115.                 case "Lot 34":
    116.                     popuptext.GetComponent<TextMesh>().text = "Lot 34\n 2.374 Acres\n 103,411.44 sq. ft\n $258,528.60";
    117.                     break;
    118.                 case "Lot 35":
    119.                     popuptext.GetComponent<TextMesh>().text = "Lot 35\n 2.272 Acres\n 98,968.32 sq. ft\n $247,420.80";
    120.                     break;
    121.                 case "Lot 36":
    122.                     popuptext.GetComponent<TextMesh>().text = "Lot 36\n 1.981 Acres\n 86,292.36 sq. ft\n $215,730.90";
    123.                     break;
    124.                 case "Lot 37":
    125.                     popuptext.GetComponent<TextMesh>().text = "Lot 37\n 3.144 Acres\n 136,952.64 sq. ft\n $308,143.44";
    126.                     break;
    127.                 case "Lot 38":
    128.                     popuptext.GetComponent<TextMesh>().text = "Lot 38\n 1.76 Acres\n 76,665.6 sq. ft\n $191,664.00";
    129.                     break;
    130.                 case "Lot 39":
    131.                     popuptext.GetComponent<TextMesh>().text = "Lot 39\n 1.656 Acres\n 72,135.36 sq. ft\n $180,338.40";
    132.                     break;
    133.                 case "Lot 40":
    134.                     popuptext.GetComponent<TextMesh>().text = "Lot 40\n 1.676 Acres\n 73,006.56 sq. ft\n $182,516.40";
    135.                     break;
    136.                 case "Lot 41":
    137.                     popuptext.GetComponent<TextMesh>().text = "Lot 41\n 1.661 Acres\n 72,353.16 sq. ft\n $180,882.90";
    138.                     break;
    139.                 case "Lot 42":
    140.                     popuptext.GetComponent<TextMesh>().text = "Lot 42\n 1.652 Acres\n 71,961.12 sq. ft\n $179,902.80";
    141.                     break;
    142.                 case "Lot 43":
    143.                     popuptext.GetComponent<TextMesh>().text = "Lot 43\n 1.755 Acres\n 76,447.8 sq. ft\n $191,119.50";
    144.                     break;
    145.                 case "Lot 44":
    146.                     popuptext.GetComponent<TextMesh>().text = "Lot 44\n 1.662 Acres\n 72,396.72 sq. ft\n $180,991.80";
    147.                     break;
    148.                 case "Lot 45":
    149.                     popuptext.GetComponent<TextMesh>().text = "Lot 45\n 1.807 Acres\n 78,712.92 sq. ft\n $196,782.30";
    150.                     break;
    151.                 case "Lot 46":
    152.                     popuptext.GetComponent<TextMesh>().text = "Lot 46\n 2.273 Acres\n 99,011.88 sq. ft\n $247,529.70";
    153.                     break;
    154.                 case "Lot 47":
    155.                     popuptext.GetComponent<TextMesh>().text = "Lot 47\n 3 Acres\n 130,680 sq. ft\n $294,030.00";
    156.                     break;
    157.             }
    158.  
    159.             textstatus="on";
    160.             GameObject lastCreated = (GameObject) Instantiate(popuptext, new Vector3(15,7), Quaternion.Euler(90,0,0));
    161.            
    162.  
    163.         }
    164.  
    165.     }
    166.     void OnMouseExit()
    167.     {
    168.        
    169.         popuptext.GetComponent<TextMesh>().text = "";
    170.         if (textstatus =="on")
    171.         {
    172.             Destroy(lastCreated);
    173.             textstatus="off";
    174.  
    175.         }
    176.  
    177.     }
    178. }
    179.  
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Your class instance of "lastCreated" is not being used; this line in the OnMouseDown method is creating a local GameObject reference called "lastCreated" instead:
    Code (CSharp):
    1. GameObject lastCreated = (GameObject) Instantiate(popuptext, new Vector3(15,7), Quaternion.Euler(90,0,0));
    Simply remove the first "GameObject" in the line to reference your class instance instead:
    Code (CSharp):
    1. lastCreated = (GameObject) Instantiate(popuptext, new Vector3(15,7), Quaternion.Euler(90,0,0));
    Next, it doesn't seem like you really need to check if your text status is "on" or "off"; you could probably just destroy your lastCreated instance and create a new instance at the same time, giving you the result you're looking for:
    Code (CSharp):
    1. void OnMouseDown() {
    2.    switch(gameObject.name) {
    3.       //all your case statements, etc...
    4.    }
    5.  
    6.    //Check if lastCreated is not null before attempting to destroy it
    7.    if(lastCreated) {
    8.       Destroy(lastCreated);
    9.    }
    10.  
    11.    //When (or if) lastCreated is destroyed, immediately assign the reference to a new popuptext object.
    12.    lastCreated = (GameObject) Instantiate(popuptext, new Vector3(15,7), Quaternion.Euler(90,0,0));
    13. }
     
  3. unity_6F9py3-5BalWIA

    unity_6F9py3-5BalWIA

    Joined:
    Aug 13, 2018
    Posts:
    3
    I tried your changes and they resulted in a few errors such as not being able to use certain lines like if(lastcreated) in your method resulted in not being able to use in c# 4.0. some other issues occured when I didn't use the text status on and off. Not sure what I need but what I really want is to just being able to remove the OnMouseExit and being able to destroy after another click is used.
     
  4. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Why dont you use your mousdown event and store each click in an int. Then when you reach a certain int value like 1, then destory the object.