Search Unity

John Lemon Tutorial Help

Discussion in 'Scripting' started by LegacyOfWax, May 28, 2019.

  1. LegacyOfWax

    LegacyOfWax

    Joined:
    Jun 12, 2014
    Posts:
    13
    Having an issue John Lemon in the 7th step. Enemies Part 1; Static Observer. step 11, Complete your Gargoyle Prefab.
    1. In the last if statement of the Update method, add the following code:
    Code (CSharp):
    1. gameEnding.CaughtPlayer ();
    You can add this here because the CaughtPlayer method is public. If it was private, your code editor would report an error with this line.

    So I did that but i get an error for it.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class GameEnding : MonoBehaviour
    7. {
    8.     public float fadeDuration = 1f;
    9.     public float displayImageDuraction = 1f;
    10.     public GameObject player;
    11.     public CanvasGroup exitBackgroundImageCanvasGroup;
    12.     public CanvasGroup caughtBackgroundImageCanvasGroup;
    13.  
    14.     bool m_IsPlayerAtExit;
    15.     bool m_IsPlayerCaught;
    16.  
    17.     float m_Timer;
    18.  
    19.     private void OnTriggerEnter(Collider other)
    20.     {
    21.         if (other.gameObject == player)
    22.         {
    23.             m_IsPlayerAtExit = true;
    24.         }
    25.     }
    26.  
    27.     public void CaughtPlayer()
    28.     {
    29.         m_IsPlayerCaught = true;
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         if (m_IsPlayerAtExit)
    35.         {
    36.             Endlevel(exitBackgroundImageCanvasGroup, false);
    37.         }
    38.         else if (m_IsPlayerCaught)
    39.         {
    40.             Endlevel(caughtBackgroundImageCanvasGroup, true);
    41.             gameEnding.CaughtPlayer();
    42.         }
    43.     }
    44.  
    45.     void Endlevel(CanvasGroup imageCanvasGroup, bool doRestart)
    46.     {
    47.         m_Timer += Time.deltaTime;
    48.  
    49.         imageCanvasGroup.alpha = m_Timer / fadeDuration;
    50.  
    51.         if (m_Timer > fadeDuration + displayImageDuraction)
    52.         {
    53.  
    54.             if (doRestart)
    55.             {
    56.                 SceneManager.LoadScene(0);
    57.             }
    58.             else
    59.             {
    60.                 Application.Quit();
    61.             }
    62.  
    63.         }
    64.     }
    65. }
    Then I though the upercase and lowercase may be wrong in the documents so i used it With GameEnding rather then gameEnding (I still got and error so not sure what to do now?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class GameEnding : MonoBehaviour
    7. {
    8.     public float fadeDuration = 1f;
    9.     public float displayImageDuraction = 1f;
    10.     public GameObject player;
    11.     public CanvasGroup exitBackgroundImageCanvasGroup;
    12.     public CanvasGroup caughtBackgroundImageCanvasGroup;
    13.  
    14.     bool m_IsPlayerAtExit;
    15.     bool m_IsPlayerCaught;
    16.  
    17.     float m_Timer;
    18.  
    19.     private void OnTriggerEnter(Collider other)
    20.     {
    21.         if (other.gameObject == player)
    22.         {
    23.             m_IsPlayerAtExit = true;
    24.         }
    25.     }
    26.  
    27.     public void CaughtPlayer()
    28.     {
    29.         m_IsPlayerCaught = true;
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         if (m_IsPlayerAtExit)
    35.         {
    36.             Endlevel(exitBackgroundImageCanvasGroup, false);
    37.         }
    38.         else if (m_IsPlayerCaught)
    39.         {
    40.             Endlevel(caughtBackgroundImageCanvasGroup, true);
    41.             GameEnding.CaughtPlayer();
    42.         }
    43.     }
    44.  
    45.     void Endlevel(CanvasGroup imageCanvasGroup, bool doRestart)
    46.     {
    47.         m_Timer += Time.deltaTime;
    48.  
    49.         imageCanvasGroup.alpha = m_Timer / fadeDuration;
    50.  
    51.         if (m_Timer > fadeDuration + displayImageDuraction)
    52.         {
    53.  
    54.             if (doRestart)
    55.             {
    56.                 SceneManager.LoadScene(0);
    57.             }
    58.             else
    59.             {
    60.                 Application.Quit();
    61.             }
    62.  
    63.         }
    64.     }
    65. }
     
    that-steve-guy likes this.
  2. LegacyOfWax

    LegacyOfWax

    Joined:
    Jun 12, 2014
    Posts:
    13
    These are to show how the error appears in VS
     

    Attached Files:

    that-steve-guy likes this.
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,638
    What error message?
     
  4. LegacyOfWax

    LegacyOfWax

    Joined:
    Jun 12, 2014
    Posts:
    13
    Error in as that it dose not work. The Photos with the red underline is where it shows that something is not right
    Visual Studios does that.

    not sure if this helps but if I have
    Code (CSharp):
    1. GameEnding.CaughtPlayer();
    it says = an object reference is required for the non-static field, method, or property "GameEnding.CaughtPlayer();"

    and if I have
    Code (CSharp):
    1. gameEnding.CaughtPlayer();
    it says = The name 'gameEnding' Does not exist in the current contect
     
    Last edited: May 28, 2019
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,638
    Not in Visual Studio. In the Unity console, it should report an error message. You can't fix the error unless you know what it is. ;)
     
  6. LegacyOfWax

    LegacyOfWax

    Joined:
    Jun 12, 2014
    Posts:
    13
    Assets\Scripts\GameEnding.cs(41,13): error CS0120: An object reference is required for the non-static field, method, or property 'GameEnding.CaughtPlayer()'
     
    that-steve-guy likes this.
  7. adibichea

    adibichea

    Joined:
    Jul 15, 2015
    Posts:
    73
    Just call CaughtPlayer();
    Replace your GameEnding.CaughtPlayer(); and/or gameEnding.CaughtPlayer(); by
    Code (CSharp):
    1. CaughtPlayer();
     
    SparrowGS likes this.
  8. effincleveland

    effincleveland

    Joined:
    Jul 21, 2015
    Posts:
    1
    Be careful which file you're updating..
    The tutorial switches between the Observer.cs file and the GameEnding.cs file pretty quick
    you want to add that bit to the Observer.cs file
     
    Last edited: Jun 10, 2019
  9. Sairam_V

    Sairam_V

    Joined:
    Jun 18, 2019
    Posts:
    1
    You have remove the GameEnding.CaughtPlayer();
    1. else if (m_IsPlayerCaught)
    2. {
    3. Endlevel(caughtBackgroundImageCanvasGroup, true);
    4. GameEnding.CaughtPlayer();
    5. }
    6. }
    and place it under observer.cs under the last if statement
    if(raycastHit.collider.transform == player)
    {
    gameEnding.CaughtPlayer();
    }
     
    CArturo likes this.
  10. that-steve-guy

    that-steve-guy

    Joined:
    Apr 21, 2013
    Posts:
    15
    I'm having the same issue.... and I went back and cut / pasted his code and still not running

    Initially - in Unity (and In Visual Studio)
    Assets\Scripts\Observer.cs(49,21): error CS0103: The name 'gameEnding' does not exist in the current context

    .... changed to GameEnding
    Assets\Scripts\Observer.cs(50,21): error CS0120: An object reference is required for the non-static field, method, or property 'GameEnding.CaughtPlayer()'




    and per fixes
    (sorry abi)
    Assets\Scripts\Observer.cs(50,18): error CS0103: The name 'CaughtPlayer' does not exist in the current context

    (sorry Sairam)
    You have remove the GameEnding.CaughtPlayer(); - did that
     
  11. AnilND

    AnilND

    Joined:
    Jul 13, 2019
    Posts:
    3
    gameEnding.CaughtPlayer();
    Here the the object "gameEnding" is undefined so you cannot call a function of an undefined object.
    Declare a game object of class "GameEnding".
    Put "public GameEnding gameEnding; in the beginning of your class. it will solve the issue.
     
  12. tasnimrushi10

    tasnimrushi10

    Joined:
    Jun 23, 2019
    Posts:
    8
    I've completed this tutorial successfully. Now I want to build the game for Android platform. Can anyone help me to write the scripts for the movement on touchscreen associated to this game.Thanks in Advance
     
  13. AnilND

    AnilND

    Joined:
    Jul 13, 2019
    Posts:
    3
    Did you figure it out?
    It is easy with four UI buttons.
     
  14. oscargil2614

    oscargil2614

    Joined:
    Apr 5, 2020
    Posts:
    2
    i have this error
    NullReferenceException: Object reference not set to an instance of an object
    PlayerMovement.OnAnimatorMove () (at Assets/Scripts/PlayerMovement.cs:34)
    step tutorial,the code is exactly in that tutorial
    Code (CSharp):
    1. void OnAnimatorMove()
    2.     {
    3.         m_Rigidbody.MovePosition(m_Rigidbody.position + m_Movement * m_Animator.deltaPosition.magnitude);
    4.         m_Rigidbody.MoveRotation(m_Rotation);
    5.     }
    regards
     
  15. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    951
    "NullReferenceException: Object reference not set to an instance of an object" means that one of your objects has the value null, which means that it is unassigned.
    The error occurs at line 34, which means that that is where the object is used.

    From the code snippet you posted, it could be m_Rigidbody, m_Movement, m_Animator or m_Rotation. Rigidbody and animator seem the most likely culprits to me.

    You have to go back to the tutorial and see how these values are assigned. This can be done in the inspector, or in code using a statement that looks something like "m_Rigidbody = GetComponent<Rigidbody>();"