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

Script being duplicated, triplicated on every Play mode

Discussion in 'Scripting' started by Evirstar, Nov 12, 2022.

  1. Evirstar

    Evirstar

    Joined:
    Nov 8, 2022
    Posts:
    3
    I'm having a weird issue, where a script will work perfectly the first time it's saved and the editor is put into play mode. It'll aim once, shoot once, stop aiming once. But if I stop the play mode in editor, then start it again, it'll double it. Aim twice, shoot twice, stop aiming twice. For each time I close the play mode then start it again, it'll add onto it, making it 3, 4, 5, etc times. Does anybody know how I can fix this?

    First time learning Unity, so apologies for any weird code. Still learning through tutorials!

    First Run:


    Second Run:


    Third Run:



    Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3. using Cinemachine;
    4.  
    5. public class SwitchVCAM : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     private PlayerInput playerInput;
    10.     [SerializeField]
    11.     private int priorityBoostAmount = 10;
    12.     [SerializeField]
    13.     private Canvas thirdPersonCanvas;
    14.     [SerializeField]
    15.     private Canvas aimCanvas;
    16.     [SerializeField]
    17.     public GameObject moveScript;
    18.     [SerializeField]
    19.     private float normSens;
    20.     [SerializeField]
    21.     private float aimSens;
    22.  
    23.     private float desiredAlpha;
    24.     private float canvasGroupAlpha;
    25.     private float moveSpeed;
    26.     private float sprintSpeed;
    27.     private bool canShoot;
    28.  
    29.     private CinemachineVirtualCamera virtualCamera;
    30.     private InputAction aimAction;
    31.     private InputAction shootAction;
    32.     private Transform cameraTransform;
    33.      
    34.      
    35.     private void Awake() {
    36.         Debug.Log("Awake");
    37.         canShoot = false;
    38.         virtualCamera = GetComponent<CinemachineVirtualCamera>();
    39.         aimAction = playerInput.actions["Aim"];
    40.         shootAction = playerInput.actions["Shoot"];
    41.         canvasGroupAlpha = aimCanvas.GetComponent<CanvasGroup>().alpha;
    42.         moveSpeed = moveScript.GetComponent<StarterAssets.ThirdPersonController>().MoveSpeed;
    43.         sprintSpeed = moveScript.GetComponent<StarterAssets.ThirdPersonController>().SprintSpeed;
    44.         cameraTransform = GameObject.Find("PlayerAimCamera").GetComponent<Transform>();
    45.  
    46.     }
    47.  
    48.     private void Update(){
    49.         canvasGroupAlpha = Mathf.MoveTowards( canvasGroupAlpha, desiredAlpha, 2.0f * Time.deltaTime);
    50.         aimCanvas.GetComponent<CanvasGroup>().alpha = canvasGroupAlpha;
    51.         moveScript.GetComponent<StarterAssets.ThirdPersonController>().MoveSpeed = moveSpeed;
    52.         moveScript.GetComponent<StarterAssets.ThirdPersonController>().SprintSpeed = sprintSpeed;
    53.     }
    54.  
    55.     private void OnEnable()
    56.     {
    57.         Debug.Log("Enable");
    58.         aimAction.performed += _ => StartAim();
    59.         aimAction.canceled += _ => CancelAim();
    60.         shootAction.performed += _ => ShootGun();  
    61.     }
    62.  
    63.     private void OnDisable()
    64.     {
    65.         Debug.Log("disable");
    66.         aimAction.performed -= _ => StartAim();
    67.         aimAction.canceled -= _ => CancelAim();
    68.         shootAction.performed -= _ => ShootGun();
    69.     }
    70.  
    71.     private void StartAim(){
    72.         Debug.Log("Start");
    73.         canShoot = true;
    74.         virtualCamera.Priority += priorityBoostAmount;
    75.         moveScript.GetComponent<StarterAssets.ThirdPersonController>().SetSensitivity(aimSens);
    76.         aimCanvas.enabled = true;
    77.         desiredAlpha = 1;
    78.         moveSpeed = 1;
    79.         sprintSpeed = 1;
    80.     }
    81.  
    82.     private void CancelAim(){
    83.         Debug.Log("Stop");
    84.         canShoot = false;
    85.         virtualCamera.Priority -= priorityBoostAmount;
    86.         moveScript.GetComponent<StarterAssets.ThirdPersonController>().SetSensitivity(normSens);
    87.         desiredAlpha = 0;
    88.         moveSpeed = 2;
    89.         sprintSpeed = 5.335f;
    90.     }
    91.  
    92.     private void ShootGun(){
    93.         if (canShoot)
    94.         {
    95.             Debug.Log("Shoot");
    96.             RaycastHit hit;
    97.             if (Physics.Raycast(cameraTransform.position, cameraTransform.forward, out hit, Mathf.Infinity))
    98.             {
    99.                 Debug.Log(hit.transform.name);
    100.             }
    101.         }
    102.     }
    103. }
    104.  
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,195
    Code (CSharp):
    1.     private void OnEnable()
    2.     {
    3.         Debug.Log("Enable");
    4.         aimAction.performed += _ => StartAim();
    5.         aimAction.canceled += _ => CancelAim();
    6.         shootAction.performed += _ => ShootGun();
    7.     }
    8.     private void OnDisable()
    9.     {
    10.         Debug.Log("disable");
    11.         aimAction.performed -= _ => StartAim();
    12.         aimAction.canceled -= _ => CancelAim();
    13.         shootAction.performed -= _ => ShootGun();
    14.     }
    This will not remove the callbacks. It will add an anonymous method as an event handler, and it will try to remove a different anonymous method (lambda) in OnDisable that just happens to call the same method.

    If I didn't make an error this should fix it:
    Code (CSharp):
    1. [code=CSharp]    private void OnEnable()
    2.     {
    3.         Debug.Log("Enable");
    4.         aimAction.performed += StartAim;
    5.         aimAction.canceled += CancelAim;
    6.         shootAction.performed += ShootGun;
    7.     }
    8.     private void OnDisable()
    9.     {
    10.         Debug.Log("disable");
    11.         aimAction.performed -= StartAim;
    12.         aimAction.canceled -= CancelAim;
    13.         shootAction.performed -= ShootGun;
    14.     }
    [/code]
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    Please don't use the General Discussion for support. Its description states as much.

    I'll move your post to the Scripting forum.
     
  4. Evirstar

    Evirstar

    Joined:
    Nov 8, 2022
    Posts:
    3
    Thank you for your help! I'm still getting another error now, for all three functions Start, Cancel and Shoot.
    error CS0123: No overload for 'StartAim' matches delegate 'Action<InputAction.CallbackContext>'
    I think this is because the functions don't have any parameters, but the InputAction does?
    If I add the parenthesis at the end it says that
    error CS0029: Cannot implicitly convert type 'void' to 'System.Action<UnityEngine.InputSystem.InputAction.CallbackContext>'

    I'm still a little lost haha, I greatly appreciate the help.

    Also apologies for posting in the wrong place!
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,956
    If you got this:

    Then it sounds like you're not doing THIS correctly:

    Let me save you a LOT of frustration and wasted time.

    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! 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.

    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.
     
  6. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    377
    performed
    callback is an
    Action<InputAction.CallbackContext>
    , which means it passes a
    InputAction.CallbackContext
    as a parameter.

    You are actually ignoring this parameter in your anonymous lambas by using
    _ =>
    (the underscore), but if you're using a method as callback, you can't simply ignore it.

    So your
    StartAnim
    method should have an
    InputAction.CallbackContext
    parameter like this:

    Code (CSharp):
    1. private void StartAnim(InputAction.CallbackContext context)
    2. {
    3.     // Do this here...
    4. }
    I don't know why a lot of developers here don't use the built-in code completion in their IDE (VS or Rider), it's just a matter of hitting Alt+Enter (or CTRL+. or clicking the light bulb) and you'll have a menu that suggests creating the callback for you, automatically, with all the necessary parameters. This also goes for a lot of other tasks, like importing namespaces, creating new methods, constructors, fields/properties, null checks, converting foreach to for and vice-versa, inverting a for loop, converting a switch to ifs and vice-versa, inverting an if (useful for creating guard clauses), renaming variables/methods (super super uselful! CTRL+R+R) and a ton of other super useful features! it even spell checks for variables and method names!

    Using the autocompletion (called IntelliSense in VS) will accelerate your development by a big margin, and avoid you a ton of bugs, headaches, Google/forums searches, a time better spent in developing your game.
     
    Last edited: Nov 13, 2022
    Evirstar likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,956
    Probably because it fails so often and people don't even realize what they're missing. Intellisense basically fails for me five to ten times a day, it's that flaky and sensitive.

    Agreed. Here's how to get it working again:

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874
     
  8. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    377
    Yes from time to time it does fail indeed, but for me it's quite rare, and generally a VS restart is all what is needed. BUT since I've switched to Rider, well a whole new world just opened for me:

    All ReSharper goodies + a lighter, faster more stable IDE + a faster more stable Debugger = 2x Productivity compared to VS. Once you get used to Rider (it'll take some time to get used to the lighter layout, different icons, toolbar items...), you can never go back to VS...
     
    Last edited: Nov 13, 2022
    Kurt-Dekker likes this.
  9. Evirstar

    Evirstar

    Joined:
    Nov 8, 2022
    Posts:
    3
    That did it, thank you! I think I understand now too. To what Kurt was mentioning, I'll give this hint in the video I was watching as I wasn't able to find anyone else having the same issue. Also a bit hard to track down since it wasn't originally giving me an error, it was just using adding the callback more every time I restarted.

    I did try this, it was attempting to help me, but it just suggested I fix formatting. No suggestions on the function lower in the script, but maybe because it was already written out beforehand?
    upload_2022-11-13_11-18-10.png


    Anyways working flawlessly now, thank you everyone!
     
  10. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    377
    Welcome.

    It doesn't show "Create method" helper because you already have a
    CancelAim
    method. If you did for eg
    += CancelAimTest
    just for testing, it'll suggest to create that method for you. Do this then compare it with yours to find where the problem is, then when you finish just remove the auto created
    CancelAimTest
    . Great way to learn.
     
    Evirstar likes this.