Search Unity

Question So i'm following a code tutorial right now and i'm working on camera shake Cinemachine.

Discussion in 'Cinemachine' started by Soemie_Unity, Jun 23, 2022.

  1. Soemie_Unity

    Soemie_Unity

    Joined:
    Jan 29, 2022
    Posts:
    4
    1 using System.Collections;
    2 using System.Collections.Generic;
    3 using UnityEngine;
    4 using Cinemachine;

    5 public class ScreenShake : MonoBehaviour
    6 {
    CinemachineBasicMultiChannelPerlin noice;
    float shakeTimer;


    void awake()
    {
    CinemachineVirtualCamera cam = GetComponent<CinemachineVirtualCamera>();
    noice = cam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
    }
    void Update()
    {
    shakeTimer = Mathf.Max (shakeTimer - Time.deltaTime, 0);
    noice.m_AmplitudeGain = shakeTimer * shakeTimer * 50;
    }

    public void Shake (float magnitude)
    {
    shakeTimer = magnitude;
    }
    }


    i keep getting this error saying:
    NullReferenceException: Object reference not set to an instance of an object
    ScreenShake.Update () (at Assets/scripts/ScreenShake.cs:20)
    i don't know how to fix this, i need help
    (cs:20 is at noice.m_Amplitude gain = shakeTimer * shakeTimer * 50; )
     
    Last edited: Jun 23, 2022
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    First of all, when pasting code, use this button. It makes things much more readable:

    upload_2022-6-23_15-30-23.png

    The error message is telling you to look at your script on line 20. Something is null. It's either your "cam" or your "noice" variable. My guess is that you don't have a CinemachineVirtualCamera component on this game object, or if you do, it doesn't have a CinemachineBasicMultiChannelPerlin subcomponent.
     
  3. Soemie_Unity

    Soemie_Unity

    Joined:
    Jan 29, 2022
    Posts:
    4
    I don't think i have that button, i'm using visual code
     
  4. Soemie_Unity

    Soemie_Unity

    Joined:
    Jan 29, 2022
    Posts:
    4
    i'm a total beginner, i did the exact thing just like the tutorial. Don't see any mistakes, here you can see everything is connected

    Yeah i'm totally new at this but, i checked everything and it seems i did the exact same thing as the tutorial has shown. Here you can see every thing is connected (sphere = player) Screenshot 2022-06-23 214917.png

    Screenshot 2022-06-23 215748.png

    also here is the improved script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Cinemachine;

    public class ScreenShake : MonoBehaviour
    {
    CinemachineBasicMultiChannelPerlin noice;
    float shakeTimer;


    void awake()
    {
    CinemachineVirtualCamera cam = GetComponent<CinemachineVirtualCamera>();
    noice = cam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
    }
    void Update()
    {
    shakeTimer = Mathf.Max (shakeTimer - Time.deltaTime, 0);
    noice.m_AmplitudeGain = shakeTimer * shakeTimer * 50;
    }

    public void Shake (float magnitude)
    {
    shakeTimer = magnitude;
    }
    }

    i don't see any null in this code so everything should be doing fine?
    i'm still getting that error
     
  5. Soemie_Unity

    Soemie_Unity

    Joined:
    Jan 29, 2022
    Posts:
    4
    that was not my intention to say something twice
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    Can you please edit your last post, delete the code, then press this button and paste your code into there? It will be much more readable.

    upload_2022-6-23_16-21-38.png
     
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    awake should be Awake so cam and noice are null
     
    Gregoryl likes this.