0

I don't know what's going on. I followed the tutorial exactly and it's still giving me an error.

No other guides on this website have helped me and I don't know what I'm doing wrong

using System.Collections.Generic;
using UnityEngine;

public class ScreenShakeController : MonoBehaviour
{
   private float shakeTimeRemaining, shakePower;

   public static void Miss()
   {
        StartShake(0.25f, 1f);
   }

   private void LateUpdate()
   {
        if(shakeTimeRemaining > 0)
        {
            shakeTimeRemaining -= Time.deltaTime;

            float xAmount = Random.Range(-1f, 1f) * shakePower;
            float yAmount = Random.Range(-1f, 1f) * shakePower;

            transform.position  += new Vector3(xAmount, yAmount, 0f);
        }
   }

   public void StartShake(float length, float power)
   {
    shakeTimeRemaining = length;
    shakePower = power;
   }
}
Jake
  • 11
  • 1
    Does this answer your question? [C# error: "An object reference is required for the non-static field, method, or property"](https://stackoverflow.com/questions/10264308/c-sharp-error-an-object-reference-is-required-for-the-non-static-field-method) – gunr2171 Aug 06 '22 at 17:17
  • Also see [a question posted a few minutes ago](https://stackoverflow.com/a/73261601/1043380) – gunr2171 Aug 06 '22 at 17:18
  • It seems that tutorial has code that is incorrect. The issue here is that you are trying to call `StartShake` in your `Miss` method, which cannot be done with your current class structure. The `static` keyword which helps to define your `Miss` method is causing the restriction. The links in other comments should help you, if they don't then perhaps these tutorials will instead: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static – Ibrennan208 Aug 06 '22 at 17:34

1 Answers1

0

Your intent here is unclear, but here is an explanation as to what the static keyword is doing here.

The problem here is that your Miss method is static, but you try to call a non-static method within it. If you wanted it to work as you have it written, you would probably have to declare all of your methods and variables as static.

Like so:

private static float shakeTimeRemaining, shakePower;
private static void LateUpdate()
public static void StartShake(float length, float power)

And then you could actually use the class name and method to call the code, rather than having to instantiate a new object, like so:

ScreenShakeController.Miss();

This is because you "use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object". Which means that you don't have a particular instance of the object that is holding the current values. Instead, those values and functions are part of the class itself (the blueprints for the object rather than the actual instantiated copy of the object).

Currently how you have it written, you would need to create a new instance of ScreenShakeController in order to use most of it, you would also have to revert the Miss method to public void Miss().

So you would need to call var exampleController = new ScreenShakeController(); And then you could use the methods that are part of that class like so:

exampleController.Miss();

Ibrennan208
  • 1,345
  • 3
  • 14
  • 31