Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why would you want to use a static method in c#? Why would it benefit the follow

ID: 3712721 • Letter: W

Question

Why would you want to use a static method in c#? Why would it benefit the following code?

using UnityEngine.UI;
using UnityEngine;


public class ScoreManager : MonoBehaviour
{
    private GameController gameController;
    public Text scoreText;
    private int score;


    void Start()
    {
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>();
        }
        if (gameController == null)
        {
            Debug.Log("Cannot find 'GameController' script");
        }

        score = 0;
        UpdateScore();
    }

    public void AddScore(int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore();
    }

    void UpdateScore()
    {
        scoreText.text = "Score: " + score;
    }

}

Explanation / Answer

Notable Points about Static Keyword are:

1) Any static method can be invoked directly from the class level, is not associated with an instance of the class.

2) For invoking any static method doesn't requires any class object and static methods are faster.

3) Any main() method which is shared through out the class scope it should always appears with static keyword.

4) A static class is a class which can only contains static members, and therefore cannot be instantiated.

5) A static variable belong to a class, and not the objects the class creates and it is shared among all class members.

A static variable retains the same value, regardless of the object from a given class and as long as the class is in the memory, it has still valid reference.

Explanation :

using UnityEngine.UI;

using UnityEngine;


public class ScoreManager : MonoBehaviour
{
    private GameController gameController;
    public Text scoreText;
    private int score;

// use this for initialization

    {
        GameObject gameControllerObject = GameObject.FindWithTag("GameController");
        if (gameControllerObject != null)
        {
            gameController = gameControllerObject.GetComponent<GameController>();
        }
        if (gameController == null)
        {

Debug.Log("Cannot find 'GameController' script");
        }

        score = 0;
        UpdateScore();
    }

    public void AddScore(int newScoreValue)
    {

// Click detected. increment score

   score += newScoreValue;

        UpdateScore();
    }

// update is called once per frame

    void UpdateScore()
    {
        scoreText.text = "Score: " + score;
    }

}

So now from above code

static methods are (i hope you can already know which are static methods):

public static GameObject FindWithTag(string tag):

This method returns one active GameObject tagged tag. Returns null if no GameObject was found.

public static void Log(object message):

This method logs message to the Unity Console.

What does This Code do:

What does AddScore() do :

when each time an object gets destroyed somepoints will be rewarded .

the AddScore() methods adds that rewarded points to the totalpoints.

and call UpdateScore().

What does UpdateScore() do :

the UpdateScore() calls once per each frame and updates the score of scoretext with updated score.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote