Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

can I use the same script function in OnClick of a single button?

Discussion in 'Scripting' started by larieflor, Jul 14, 2022.

  1. larieflor

    larieflor

    Joined:
    Jul 2, 2022
    Posts:
    8
    I have a function needed by using only click a single button: to SaveAnswer and GetData. both uses the same scene and script. Is this possible?(Image below) I'm a newb at c# like I've never used it before. but i have a question manager script that i applied to canvas scene and to the button scene inside that canvas. the button scene has multiple functions.but when i run it it says it has errors like:
    Assets\scenes\QuestionManager.cs(35,13): error CS0128: A local variable or function named 'q2' is already defined in this scope


    https://imgur.com/gallery/pj4ln1P


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class QuestionManager : MonoBehaviour
    7. {
    8.     public string questionID;
    9.     public string question;
    10.     public bool isYes;
    11.     public TMP_Text textQuestion;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     public void SaveQuestionAnswer(int value){
    19.         if(isYes){
    20.             PlayerPrefs.SetInt(questionID, value);          
    21.         }else{
    22.             PlayerPrefs.SetInt(questionID, value);
    23.         }
    24.         Debug.Log("questionID: "+questionID+" value: "+value);
    25.     }
    26.  
    27.     public void GetData(string questionID){
    28.         int result = PlayerPrefs.GetInt(questionID);
    29.         Debug.Log("result: "+result);
    30.  
    31.         int q1 = PlayerPrefs.GetInt("q1");
    32.         int q2 = PlayerPrefs.GetInt("q2");
    33.         int q2 = PlayerPrefs.GetInt("q3");
    34.         int q2 = PlayerPrefs.GetInt("q4");
    35.         int q2 = PlayerPrefs.GetInt("q5");
    36.         int q2 = PlayerPrefs.GetInt("q6");
    37.         int q2 = PlayerPrefs.GetInt("97");
    38.         int q2 = PlayerPrefs.GetInt("q8");
    39.         int q2 = PlayerPrefs.GetInt("q9");
    40.         int q10 = PlayerPrefs.GetInt("q10");
    41.  
    42.         int sum = q1+q2+q3+q4+q5+q6+q7+q8+q9+q10;
    43.     }
    44.  
    45.  
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.         textQuestion.text = question;
    51.     }
    52. }
    53.  
     
  2. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    The error message already tells you the problem. You try to define a variable with a name that already exist.
    You defined 8 times a variable with the same name (line 32 -39) - q2.

    Wether you define the variable one time and overwrite it (what doesn´t make any sense in your case):
    Code (CSharp):
    1. int q2 = PlayerPrefs.GetInt("q2");
    2. q2 = PlayerPrefs.GetInt("q3");
    3. q2 = PlayerPrefs.GetInt("q4");
    4. ....
    or what you want to have, I guess: change the name of the variables from line 33 - 39 in q3, q4 ...
     
    Last edited: Jul 14, 2022
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    What @Sphinks says above.

    Screen Shot 2022-07-14 at 6.32.52 AM.png

    ALSO... look into some kind of structured data storage like JSON.

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io
     
  4. larieflor

    larieflor

    Joined:
    Jul 2, 2022
    Posts:
    8
    thank you so much! will look into this! :)