Anyone have tips on making an item shake after a wrong guess?

Basically I have a cube item in my scene, and another script tracking correct and incorrect guesses. Been following a lot of guides to get to this point, so I feel kind of in the dark.

Anyway, I want the cube to shake each time the player inputs an incorrect guess. Anyone got tips on how to do that? Where should I start looking?

Just animate it and play the animation.

2 Likes

Do you know how to do this across scripts? Basically:

public void Check()
    {
       if
        {
            OnWordCheckCorrect?.Invoke(); 
            _inputField.text = "";
         //    isleShake.SetTrigger("TrShake")
        }
    }

the committed out bit didn’t work. here is the script attached to the shaking item.

 public Animator isleShake;
    void Start()
    {
        isleShake = GetComponent<Animator>();
    }
    void Update()
    {
    }

the script and animation are attached to the shaking item, the thing that should trigger the shaking is elsewhere (i can’t attach it to the shaking item, not without a lot of code restructuring – hoping to avoid that)

Referencing variables, fields, methods (anything non-static) in other script instances:

REMEMBER: it isn’t always the best idea for everything to access everything else all over the place. For instance, it is not great for the player to reach into an enemy and reduce his health.

Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

Thanks. But, I am now getting a “Object reference not set to an instance of an object” error.

// public PenaltyIsle shakeIt;
.
.
.
public void Check()
    {
       if
        {
            OnWordCheckCorrect?.Invoke();
            _inputField.text = "";
             // shakeIt.Shake();
        }
    }
public class PenaltyIsle : MonoBehaviour
{
    public Animator isleShake;
    void Start()
    {
        isleShake = GetComponent<Animator>();
    }
  //  public void Shake()
  //  {
 //       isleShake.SetTrigger("TrShake");
 //   }
}

Those NEVER EVER EVER require a forum post.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that
1 Like

NEVERMIND I got it figured out. Thanks to all.