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. Dismiss Notice

Resolved Private methods are unused

Discussion in 'Scripting' started by Ch267, Jul 14, 2021.

Thread Status:
Not open for further replies.
  1. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    As the title says, the private methods Awake, Update, and OnCollisionEnter are not being read by the computer, causing my game to initially read them, but after the scene is loaded upon clicking the 'Restart'button after either a game over or a victory, it doesn't read the PlayerMovement script. Help with this issue would be greatly appreciated.
    For reference, here is the PlayerMovement script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float speed;
    8.     Rigidbody2D rb2D;
    9.     private Vector2 jumpForce;
    10.     private Vector2 left;
    11.     private Vector2 right;
    12.     int totalJumps = 3; // the player will have three jumps avaliable to them
    13.     int jumps; // these are the extra jumps the player has at their disposal
    14.     public GameObject gameController;
    15.     // Start is called before the first frame update
    16.     void Awake()
    17.     {
    18.         jumps = totalJumps;
    19.         rb2D = gameObject.GetComponent<Rigidbody2D>();
    20.         jumpForce = new Vector2(0.0f, 5.5f);
    21.         left = new Vector2(-1.0f, 0.0f);
    22.         right = new Vector2(1.0f, 0.0f);
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if (Input.GetKeyDown(KeyCode.Space)&& jumps > 0)
    29.         {
    30.             rb2D.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
    31.             jumps--; //take away a jump each time one is used
    32.         }
    33.  
    34.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    35.         {
    36.             rb2D.AddForce(left * speed, ForceMode2D.Impulse);
    37.         }
    38.  
    39.         if (Input.GetKeyDown(KeyCode.RightArrow))
    40.         {
    41.             rb2D.AddForce(right * speed, ForceMode2D.Impulse);
    42.         }
    43.     }
    44.  
    45.     private void OnCollisionEnter2D(Collision2D collision)
    46.     {
    47.         if (collision.gameObject.layer == 3)
    48.         {
    49.             jumps = totalJumps;
    50.         }
    51.  
    52.         if (collision.gameObject.CompareTag("Victory"))
    53.         {
    54.             gameController.GetComponent<GameController>().win = true;
    55.         }
    56.     }
    57.  
    58. }
    59.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    What do you mean, "it doesn't read the PlayerMovement script"? Are you saying that Awake, Update, and OnCollisionEnter are never called after you hit Restart?

    Is this component still enabled and on an active GameObject after you hit Restart? When you hit Restart, does this object get destroyed and recreated or just disabled until you restart?
     
    Ch267 and Bunny83 like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,279
    What does this mean? The code doesn't go away.

    Are you seeing an error? Then follow this:

    Remember: NOBODY memorizes error codes. 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.

    Always start with the FIRST error in the list, as sometimes that error causes or compounds some or all of the subsequent errors.

    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)

    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.

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    Otherwise if there is no error, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    Bunny83 likes this.
  4. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    I checked and the script in question is never destroyed or disabled. The Awake, Update, and OnCollisionEnter methods are never read upon restarting, it runs as if the code doesn't exist at all.
     
    abhi1abhisheksaha likes this.
  5. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    All the errors I'm attempting to address are exactly what the title says "Private member unused", these private members being Awake. Update, and OnCollisionEnter2D.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,279
    Those should be warnings (eg, yellow and don't prohibit running), not errors.

    The visual studio integration is supposed to know NOT to warn about those being unused, since that's just how Unity works. Ideally those would not be warnings if it was set up right.

    Sometimes all that is necessary to fix this is to do Assets -> Open C# Project from within Unity.

    But sometimes it takes more. Here is more on how to check on the VS integration:

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

    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
     
    Gamivision, Ch267 and Bunny83 like this.
  7. Ch267

    Ch267

    Joined:
    Sep 16, 2019
    Posts:
    55
    Thank you! The methods are being read as normal now but still not functioning correctly after I press the "Restart" button. Before I fiddle around with the forum hoping to solve that issue, I'll check the Restart function itself, I'll check the button, and insert a few Debug.Log() statements to make sure the methods above are working fine.
     
    Kurt-Dekker likes this.
  8. WILEz1975

    WILEz1975

    Joined:
    Mar 23, 2013
    Posts:
    368
    A true gem. Thanks.
     
    Kurt-Dekker likes this.
  9. Gamivision

    Gamivision

    Joined:
    Apr 23, 2021
    Posts:
    6

    Works for me
     
Thread Status:
Not open for further replies.