Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question 4 buttons generate 1 number each. When all generated 5 button send the numebers to Paramater. Stuck

Discussion in 'Scripting' started by VioPlayer, Oct 20, 2023.

  1. VioPlayer

    VioPlayer

    Joined:
    Oct 17, 2023
    Posts:
    3
    Hi,
    I'm new to C# and trying to create something in Unity/FMOD. I'm confident in FMOD but new to Unity and C#. Any help would be really appreciated. FMOD and Unity are connected properly. Unity version 2022.3.11f1 silicon.

    I have a UI canvas with x4 UI buttons in it. Each button needs to send a random number between 0-3 when the button is pressed. Once all 4 buttons have been pressed a 5th button will then send those numbers to FMOD so the parameter is triggered by all 4 numbers at the same time. This generates an audio event. The four numbers need then to be stored for later use in the game. The next time the 4 buttons are pressed a new combination is generated, sent to FMOD which generates the sound and the new numbers are stored. There is no limit to how many times this can happen.

    I've tried some versions of this and they're I'm coming up with errors. Here's the code so far.

    using UnityEngine;
    using UnityEngine.InputSystem;
    using FMODUnity;

    public class ButtonController : MonoBehaviour
    {
    private InputAction generateStem1;
    private InputAction generateStem2;
    private InputAction generateStem3;
    private InputAction generateStem4;
    private InputAction triggerFMOD;

    private void Awake()
    {
    generateStem1 = new InputAction(binding: "<Keyboard>/g");
    generateStem2 = new InputAction(binding: "<Keyboard>/h");
    generateStem3 = new InputAction(binding: "<Keyboard>/j");
    generateStem4 = new InputAction(binding: "<Keyboard>/k");
    triggerFMOD = new InputAction(binding: "<Keyboard>/f");
    }

    private void OnEnable()
    {
    // Enable the input actions.
    generateStem1.Enable();
    generateStem2.Enable();
    generateStem3.Enable();
    generateStem4.Enable();
    triggerFMOD.Enable();
    }

    private void OnDisable()
    {
    // Disable the input actions when the script is disabled.
    generateStem1.Disable();
    generateStem2.Disable();
    generateStem3.Disable();
    generateStem4.Disable();
    triggerFMOD.Disable();
    }

    [InputAction("GenerateStem1")]
    public void OnGenerateStem1(InputAction.CallbackContext context)
    {
    if (context.performed)
    {
    int randomValue = Random.Range(0, 4);
    // Handle the random number for button 1.
    }
    }

    [InputAction("GenerateStem2")]
    public void OnGenerateStem2(InputAction.CallbackContext context)
    {
    if (context.performed)
    {
    int randomValue = Random.Range(0, 4);
    // Handle the random number for button 2.
    }
    }

    [InputAction("GenerateStem3")]
    public void OnGenerateStem3(InputAction.CallbackContext context)
    {
    if (context.performed)
    {
    int randomValue = Random.Range(0, 4);
    // Handle the random number for button 3.
    }
    }

    [InputAction("GenerateStem4")]
    public void OnGenerateStem4(InputAction.CallbackContext context)
    {
    if (context.performed)
    {
    int randomValue = Random.Range(0, 4);
    // Handle the random number for button 4.
    }
    }

    [InputAction("TriggerFMOD")]
    public void OnTriggerFMOD(InputAction.CallbackContext context)
    {
    if (context.performed)
    {
    // Trigger FMOD with the stored random numbers simultaneously.
    // Handle triggering FMOD parameters here.
    }
    }

    // Other methods for handling other button clicks.
    }


    I'm getting "InputAction" is not an attribute class (CS0616) error for lines 42,52,62,72 and 82.

    Help gratefully received.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,298
    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - ONLY post the relevant code, and then refer to it in your discussion.



    You can fix your own typing mistakes. Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.


    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! See above!
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,758
    Well, you're trying to use InputAction like an attribute, and it isn't one. Check the documentation for InputAction - it has plenty of sample code, and none of it looks like the lines generating those errors.
     
  4. VioPlayer

    VioPlayer

    Joined:
    Oct 17, 2023
    Posts:
    3
    Thanks StarManta, Will have a look through the documentation.
     
  5. VioPlayer

    VioPlayer

    Joined:
    Oct 17, 2023
    Posts:
    3
    All sorted. Thanks for you help.