Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

[UNITY 5 FPS Tutorials] GTGD S3 - Advanced First Person Shooter

Discussion in 'Community Learning & Teaching' started by GTGD, Oct 9, 2015.

  1. vladalbescu

    vladalbescu

    Joined:
    Jun 8, 2017
    Posts:
    1


    My attempt at the S3 Chapter 1 challenge. For unknown reasons, it didn't record from the beginning of the game, when there were no evil cubes and all was chill.
     
    GTGD and KUFgoddess like this.
  2. KUFgoddess

    KUFgoddess

    Joined:
    Apr 2, 2015
    Posts:
    23
    Does anyone know how to add health to the player on trigger enter?? My player walks across a box to receive health. I am on chapter 9 and I have been trying to do this for weeks and no luck I feel like im very warm but it just is giving me so much trouble ive tried so many combinations. Like I keep looking at how the enemy script is set up and im trying to do it like but no success.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine;
    6.  
    7. namespace SpaceHorror
    8. {
    9.     public class Player_Health : MonoBehaviour
    10.     {
    11.         private GameManager_Master gameManagerMaster;
    12.         private Player_Master playerMaster;
    13.         public int playerHealth;
    14.         public string Oddity;
    15.         public Text healthText;
    16.  
    17.  
    18.  
    19.         void OnTriggerEnter(Collider other)
    20.         {
    21.             if (other.CompareTag("Player"))
    22.             {
    23.                 IncreaseHealth();  //<--- this is making it not compile but im not sure if this even makes sense.
    24.  
    25.  
    26.  
    27.             }
    28.  
    29.             SetUI();
    30.         }
    31.                 //void OnTriggerEnter(Collider other)
    32.                 //{
    33.                 //    if (other.CompareTag("Player"))
    34.                 //    {
    35.                 //        playerMaster.CallEventPlayerHealthIncrease = GetComponent<Player_Master>();
    36.  
    37.                 //        Player_Master playerMaster = GetComponent<OtherScript>();
    38.                 //        otherScript.DoSomething();
    39.                 //        //playerMaster.EventPlayerHealthIncrease += IncreaseHealth;
    40.                 //        SetUI();
    41.  
    42.                 //        //playerMaster.GetComponent<Player_Master>().EventPlayerHealthIncrease(increase);
    43.                 //    }
    44.                 //    {
    45.                 //        if (playerHealth > 100)
    46.                 //        {
    47.  
    48.                 //        }
    49.  
    50.  
    51.                 //        //playerMaster.GetComponent<Player_Master>().EventPlayerHealthIncrease(increase);
    52.  
    53.  
    54.                 //    }
    55.                 //}
    56.  
    57.  
    58.                 void OnEnable()
    59.         {
    60.             SetInitialReferences();
    61.             SetUI();
    62.             playerMaster.EventPlayerHealthDeduction += DeductHealth;
    63.             playerMaster.EventPlayerHealthIncrease += IncreaseHealth;
    64.         }
    65.  
    66.         void OnDisable()
    67.         {
    68.             playerMaster.EventPlayerHealthDeduction -= DeductHealth;
    69.             playerMaster.EventPlayerHealthIncrease -= IncreaseHealth;
    70.         }
    71.  
    72.         //void Start()
    73.         //{
    74.         //    StartCoroutine(TestHealthIncrease());
    75.         //}
    76.  
    77.      
    78.         void SetInitialReferences()
    79.         {
    80.             gameManagerMaster = GameObject.Find("GameManager").GetComponent<GameManager_Master>();
    81.             playerMaster = GetComponent<Player_Master>();
    82.         }
    83.  
    84.         //IEnumerator TestHealthIncrease()
    85.         //{
    86.         //    yield return new WaitForSeconds(4);
    87.         //   IncreaseHealth(300);
    88.         //    playerMaster.CallEventPlayerHealthIncrease(1);
    89.         //}
    90.  
    91.         void DeductHealth(int healthChange)
    92.         {
    93.             playerHealth -= healthChange;
    94.             if(playerHealth<=0)
    95.             {
    96.                 playerHealth = 0;
    97.                 gameManagerMaster.CallEventGameOver();
    98.                 SceneManager.LoadScene(Oddity);
    99.  
    100.  
    101.             }
    102.  
    103.             SetUI();
    104.  
    105.         }
    106.  
    107.  
    108.  
    109.  
    110.         void IncreaseHealth(int healthChange)
    111.         {
    112.  
    113.             playerHealth += healthChange;
    114.  
    115.             if(playerHealth>=100)
    116.             {
    117.                 playerHealth = 100;
    118.             }
    119.  
    120.             SetUI();
    121.         }
    122.  
    123.  
    124.  
    125.      
    126.  
    127.  
    128.         void SetUI()
    129.         {
    130.             healthText.text = playerHealth.ToString();
    131.         }
    132.     }
    133. }
     
  3. KUFgoddess

    KUFgoddess

    Joined:
    Apr 2, 2015
    Posts:
    23
    Hey did anyone figure out how to ADD health to the player?
     
  4. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    415
    YouTube will help you with good tutorial.
     
  5. VCGhost

    VCGhost

    Joined:
    Jun 18, 2017
    Posts:
    5
    Error.png
    thank you for you nice teaching style I found your tutorials really nice
    would you please tell me that why is this error appearing I wrote the same script as you wrote in video for menu UI script
     
  6. VCGhost

    VCGhost

    Joined:
    Jun 18, 2017
    Posts:
    5
    and this is the script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. namespace S3
    5. {
    6.     public class GameManager_ToggleMenu : MonoBehaviour
    7.     {
    8.         private GameManager_Master gameManagerMaster;
    9.         public GameObject menu;
    10.  
    11.  
    12.         // Use this for initialization
    13.         void Start()
    14.         {
    15.             ToggleMenu();
    16.         }
    17.  
    18.         // Update is called once per frame
    19.         void Update()
    20.         {
    21.             CheckForToggleMenuRequest();
    22.         }
    23.         void OnEabled()
    24.         {
    25.             SetInitialRefrences();
    26.             gameManagerMaster.GameOverEvent += ToggleMenu;
    27.         }
    28.  
    29.         void OnDisabled()
    30.         {
    31.             gameManagerMaster.GameOverEvent -= ToggleMenu;
    32.         }
    33.  
    34.         void SetInitialRefrences()
    35.         {
    36.             gameManagerMaster = GetComponent<GameManager_Master>();
    37.         }
    38.  
    39.         void CheckForToggleMenuRequest()
    40.         {
    41.             if (Input.GetKeyUp(KeyCode.Escape) && !gameManagerMaster.isGameOver && !gameManagerMaster.isInventoryUIOn)
    42.             {
    43.                 ToggleMenu();
    44.             }
    45.         }
    46.  
    47.         void ToggleMenu()
    48.         {
    49.             if (menu != null)
    50.             {
    51.                 menu.SetActive(!menu.activeSelf);
    52.                 gameManagerMaster.isMenuOn = !gameManagerMaster.isMenuOn;
    53.                 gameManagerMaster.CallEventMenuToggle();
    54.             }
    55.             else
    56.             {
    57.                 Debug.LogWarning("You Need To Assign a UI Game Object To The Toggle Menu Script In Inspector.");
    58.             }
    59.         }
    60.     }
    61. }
     
  7. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    415
    Put this issue on this section.
    https://forum.unity3d.com/forums/scripting.12/
     
  8. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    429
    Look at the name of your OnEnable and OnDisable methods. They are Unity methods and must match the Unity names exactly or else they are never called
     
  9. VCGhost

    VCGhost

    Joined:
    Jun 18, 2017
    Posts:
    5
    really thanks for your help and the attention that you have for answering questions
     
  10. VCGhost

    VCGhost

    Joined:
    Jun 18, 2017
    Posts:
    5
    Hi
    Thank you for your great tutorials
    Would you please tell me how can I solve this error
    It's complaining about Item Ammo script and it's the error
    Error.png
    I really need you help
     
  11. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    429
    Double click on the error to go to the potential problem area in the code. Basically it is saying that the script is trying to get information from an index number in the list that doesn't even exist. For example the list only has two entries but you are trying to access the third entry, but there is no third entry because the list only has two!
     
  12. mattneed

    mattneed

    Joined:
    Jun 25, 2017
    Posts:
    1
    I know this is a really old thread but is there a chance there is an update somewhere for unity 5 so i can do all the lessons?
     
  13. cashleycool

    cashleycool

    Joined:
    May 31, 2017
    Posts:
    2
    Hey guys, this time is regarding the 150th video. I have got no problem with the code in void Patrol() and have manually assigned waypoints; so the enemy is patrolling just fine.

    However, I wanted the enemy to pause in between waypoints for a few seconds whenever it reaches its destination; like how it is seen when allowing enemies to randomly wander (like in bool RandomWanderTarget). I was going to use StartCoroutines, but discovered quickly that it would never work since the NPCState_Patrol script does not utilize MonoBehaviour.

    Whatever should I do? Add another bool then calling it in void Patrol()?

    If anybody has the time to help, that would be really awesome. Sorry for the inconvenience people!
     
  14. Scorpion005

    Scorpion005

    Joined:
    Jul 3, 2017
    Posts:
    2
    Hi and thanks for your tutorials
    I've got a problem with enemy detection code the error in the picture Picture.jpg
    and this is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. namespace S3
    6. {
    7.     public class Enemy_Detection : MonoBehaviour {
    8.  
    9.         private Enemy_Master enemyMaster;
    10.         private Transform myTransform;
    11.         public Transform head;
    12.         public LayerMask playerLayer;
    13.         public LayerMask sightLayer;
    14.         private float checkRate;
    15.         private float nextCheck;
    16.         public float detectRadius;
    17.         private RaycastHit hit;
    18.  
    19.         void OnEnable()
    20.         {
    21.             SetInitialRefrences();
    22.             enemyMaster.EventEnemyDie+=DisableThis;
    23.            
    24.         }
    25.  
    26.  
    27.  
    28.         void OnDisable()
    29.         {
    30.             enemyMaster.EventEnemyDie -= DisableThis;
    31.         }
    32.  
    33.  
    34.         void Update ()
    35.         {
    36.             CarryOutDetection();
    37.         }
    38.  
    39.  
    40.         void SetInitialRefrences()
    41.         {
    42.             enemyMaster = GetComponent<Enemy_Master>();
    43.             myTransform = transform;
    44.             if (head = null)
    45.             {
    46.                 head = myTransform;
    47.             }
    48.  
    49.             checkRate = Random.Range(0.8f, 1.2f);
    50.         }
    51.  
    52.         void CarryOutDetection()
    53.         {
    54.             if (Time.time > nextCheck)
    55.             {
    56.                 nextCheck = Time.time + checkRate;
    57.  
    58.  
    59.                 Collider[] colliders = Physics.OverlapSphere(myTransform.position, detectRadius, playerLayer);
    60.  
    61.                 if (colliders.Length > 0)
    62.                 {
    63.                     foreach (Collider target in colliders)
    64.                     {
    65.                         if (target.CompareTag(GameManager_Refrences._playerTag))
    66.                         {
    67.                             if (CanBeseen(target.transform))
    68.                             {
    69.                                 break;
    70.                             }
    71.                         }
    72.                     }
    73.                 }
    74.  
    75.                 else
    76.                 {
    77.                     enemyMaster.CallEventEnemyLostTarget();
    78.                 }
    79.             }
    80.         }
    81.  
    82.         bool CanBeseen(Transform target)
    83.         {
    84.             if (Physics.Linecast(head.position, target.position, out hit, sightLayer))
    85.             {
    86.                 if (hit.transform == target)
    87.                 {
    88.                     enemyMaster.CallEventEnemySetNavTarget(target);
    89.                     return true;
    90.                 }
    91.                 else
    92.                 {
    93.                     enemyMaster.CallEventEnemyLostTarget();
    94.                     return false;
    95.                 }
    96.             }
    97.             else
    98.             {
    99.                 enemyMaster.CallEventEnemyLostTarget();
    100.                 return false;
    101.             }
    102.  
    103.  
    104.         }
    105.        
    106.         void DisableThis()
    107.         {
    108.             this.enabled = false;
    109.         }
    110.     }
    111. }
    also this is my enemy master code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. namespace S3
    6. {
    7.     public class Enemy_Master : MonoBehaviour
    8.     {
    9.  
    10.         public Transform myTarget;
    11.  
    12.         public delegate void GeneralEventHandler();
    13.         public event GeneralEventHandler EventEnemyDie;
    14.         public event GeneralEventHandler EventEnemyWalking;
    15.         public event GeneralEventHandler EventEnemyReachedNavTarget;
    16.         public event GeneralEventHandler EventEnemyAttack;
    17.         public event GeneralEventHandler EventEnemyLostTarget;
    18.  
    19.         public delegate void HealthEventHandler(int health);
    20.         public event HealthEventHandler EventEnemyDeductHealth;
    21.  
    22.         public delegate void NavTargetEventHandler(Transform targetTransform);
    23.         public event NavTargetEventHandler EventEnemySetNavTarget;
    24.  
    25.         public void CallEventEnemyDeductHealth(int health)
    26.         {
    27.             if (EventEnemyDeductHealth != null)
    28.             {
    29.                 EventEnemyDeductHealth(health);
    30.             }
    31.         }
    32.  
    33.         public void CallEventEnemySetNavTarget(Transform targTransform)
    34.         {
    35.             if (EventEnemySetNavTarget != null)
    36.             {
    37.                 EventEnemySetNavTarget(targTransform);
    38.             }
    39.  
    40.             myTarget = targTransform;
    41.         }
    42.  
    43.         public void CallEventEnemyDie()
    44.         {
    45.             if (EventEnemyDie != null)
    46.             {
    47.                 EventEnemyDie();
    48.             }
    49.         }
    50.  
    51.         public void CallEventEnemyWalking()
    52.         {
    53.             if (EventEnemyWalking != null)
    54.             {
    55.                 EventEnemyWalking();
    56.             }
    57.         }
    58.  
    59.         public void CallEventEnemyReachedNavTarget()
    60.         {
    61.             if (EventEnemyReachedNavTarget != null)
    62.             {
    63.                 EventEnemyReachedNavTarget();
    64.             }
    65.         }
    66.  
    67.  
    68.         public void CallEventEnemyAttack()
    69.         {
    70.             if (EventEnemyAttack != null)
    71.             {
    72.                 EventEnemyAttack();
    73.             }
    74.         }
    75.  
    76.         public void CallEventEnemyLostTarget()
    77.         {
    78.             if (EventEnemyLostTarget != null)
    79.             {
    80.                 EventEnemyLostTarget();
    81.             }
    82.             myTarget = null;
    83.         }
    84.     }
    85. }
    help me pelase
     
  15. BIGTIMEMASTER

    BIGTIMEMASTER

    Joined:
    Jun 1, 2017
    Posts:
    1,535
    Wondering if anybody has encountered this problem and has a solution:

    Video 32, I've completed the GameManagerMaster, GameManagerPauseToggle, and GameMangerToggleMenu scripts. Been over every line and it all runs without any compilers errors, yet the time will not pause when the menu is active. I can still move around.


    UPDATE:

    Couldn't find any solution to this problem, so I just continued on. Once I added the Player Toggle script it started working as expected, though somehow now that is messing with the cursor lock -- disabling it.
     
    Last edited: Jul 9, 2017
  16. Yobeeno

    Yobeeno

    Joined:
    Jul 22, 2017
    Posts:
    2
    Added my Project 1 Assignment video on YouTube. Hope you like.
     
  17. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    429
    A++ Really Excellent!
    The ending boss fight is a nice touch
     
  18. Yobeeno

    Yobeeno

    Joined:
    Jul 22, 2017
    Posts:
    2
    Wow - Thanks very much! BTW Your tutorials are top notch too. Thanks for sharing!
     
  19. JemoYT

    JemoYT

    Joined:
    Feb 13, 2017
    Posts:
    7
    Is it normal for the burst fire to be active without the hasburstfire bool active?
     
  20. Scorpion005

    Scorpion005

    Joined:
    Jul 3, 2017
    Posts:
    2
    did any one find the answer?
     
  21. marcelolecram

    marcelolecram

    Joined:
    Jan 22, 2016
    Posts:
    3
    Thanks a lot for the course!
    It really help me a lot
    anyway this is my assignment for chapter 1

    PS i suck at art :p
     
  22. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    429
    A+, Great work!
    I really like the moving platforms! Sorry I didn't see your video sooner, I've been away for a bit.
     
  23. Saurabh-Saralaya

    Saurabh-Saralaya

    Joined:
    Apr 28, 2016
    Posts:
    23
    Is there a way to attach, IEnumerators onto Events?

    Code (CSharp):
    1. itemMaster.EventObjectThrow += DestroyUsingTimer;
    2.  
    3. //to
    4.  
    5. itemMaster.EventObjectThrow += BeginExplosionTimer;
    6.  
    Code (CSharp):
    1.         void DestroyUsingTimer()
    2.         {
    3.             //new WaitForSeconds(waitTime);
    4.  
    5.             if (GetComponent<Item_Pickup>() != null)
    6.             {
    7.                 GetComponent<Item_Pickup>().enabled = false;
    8.             }
    9.  
    10.             StartCoroutine(BeginExplosionTimer());
    11.         }
    12.  
    13.         IEnumerator BeginExplosionTimer()
    14.         {
    15.             yield return new WaitForSeconds(waitTime);
    16.             destructibleMaster.CallEventDestroyMe();
    17.         }
    I think it could be simplified into a single function
    But WaitForSeconds() did not execute in the function.
     
  24. Saurabh-Saralaya

    Saurabh-Saralaya

    Joined:
    Apr 28, 2016
    Posts:
    23
    I published my game, on IndieDB
    It's a Procedurally generated Arcade titled Expunge!

    Download it here: http://www.indiedb.com/games/expunge

    I did not use Evil max, Portable Hut or Vehicles just yet, probably soon :p
     
    Last edited: Oct 4, 2017
  25. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    429
    Well that is it everyone, all good things have to come to an end, and video 210 is the final video for GTGD S3 :)
    Thanks for being part of an incredible journey and I hope you have benefited a lot. I'll certainly be making an effort to see your assignments and play your games. Saurabh's game is on my list of games to play :).

    Btw, I'm working on the new GTGD S1, Unity 2017 multiplayer tutorial. You'll see a video for it soon hopefully. It's very different from the original S1 but I believe a lot more useful and I've made it more focused on the important and more commonly used aspects of the Unity multiplayer system. You can try out the built project now. You can download it from my website here. The new GTGD S1 will replace the product on Steam. While I might release a few of the new S1 videos on YouTube, the complete tutorial will be available through Steam.
     
    Saurabh-Saralaya and Sonnenspeer like this.
  26. Smiderplanen

    Smiderplanen

    Joined:
    Sep 11, 2017
    Posts:
    2
    i really need ur help i get this error in console when trying to pickup ammo:

    NullReferenceException: Object reference not set to an instance of an object
    Item_Ammo.TakeAmmo () (at Assets/Scripts/Item Scripts/Item_Ammo.cs:62)


    Here is my Item_Ammo Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Item_Ammo : MonoBehaviour
    6. {
    7.     private Item_Master itemMaster;
    8.     private GameObject playerGo;
    9.     public string ammoName;
    10.     public int quantity;
    11.     public bool isTriggerPickup;
    12.  
    13.     void OnEnable()
    14.     {
    15.         SetInitialReferences();
    16.         itemMaster.EventObjectPickup += TakeAmmo;
    17.     }
    18.  
    19.     void OnDisable()
    20.     {
    21.         itemMaster.EventObjectPickup -= TakeAmmo;
    22.     }
    23.  
    24.     void Start()
    25.     {
    26.         SetInitialReferences();
    27.     }
    28.  
    29.     void OnTriggerEnter(Collider other)
    30.     {
    31.         if (other.CompareTag(GameManager_References._playerTag) && isTriggerPickup)
    32.         {
    33.             TakeAmmo();
    34.         }
    35.             if (other.CompareTag(GameManager_References._playerTag))
    36.             {
    37.                 TakeAmmo();
    38.             }
    39.         }
    40.  
    41.     void SetInitialReferences()
    42.     {
    43.         itemMaster = GetComponent<Item_Master>();
    44.         playerGo = GameManager_References._player;
    45.  
    46.         if (isTriggerPickup)
    47.         {
    48.             if (GetComponent<Collider>() != null)
    49.             {
    50.                 GetComponent<Collider>().isTrigger = true;
    51.             }
    52.  
    53.             if (GetComponent<Rigidbody>() != null)
    54.             {
    55.                 GetComponent<Rigidbody>().isKinematic = true;
    56.             }
    57.         }
    58.     }
    59.  
    60.     void TakeAmmo()
    61.     {
    62.         playerGo.GetComponent<Player_Master>().CallEventPickedUpAmmo(ammoName, quantity);
    63.         Destroy(gameObject);
    64.     }
    65. }
    66.  
    Here is my Item_Master Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Item_Master : MonoBehaviour
    6. {
    7.     private Player_Master playerMaster;
    8.     public GameObject playerController;
    9.  
    10.     public delegate void GeneralEventHandler();
    11.     public event GeneralEventHandler EventObjectThrow;
    12.     public event GeneralEventHandler EventObjectPickup;
    13.  
    14.     public delegate void PickupActionEventHandler(Transform item);
    15.     public event PickupActionEventHandler EventPickupAction;
    16.  
    17.     void OnEnable()
    18.     {
    19.         SetInitialReferences();
    20.     }
    21.  
    22.     void Start()
    23.     {
    24.         SetInitialReferences();
    25.     }
    26.  
    27.     public void CallEventObjectThrow()
    28.     {
    29.         if (EventObjectThrow != null)
    30.         {
    31.             EventObjectThrow();
    32.         }
    33.         playerMaster.CallEventHandsEmpty();
    34.         playerMaster.CallEventInventoryChanged();
    35.     }
    36.  
    37.     public void CallEventObjectPickup()
    38.     {
    39.         if (EventObjectPickup != null)
    40.         {
    41.             EventObjectPickup();
    42.         }
    43.         playerMaster.CallEventInventoryChanged();
    44.     }
    45.  
    46.     public void CallEventPickupAction(Transform item)
    47.     {
    48.         if (EventPickupAction != null)
    49.         {
    50.             EventPickupAction(item);
    51.  
    52.         }
    53.     }
    54.  
    55.     void SetInitialReferences()
    56.     {
    57.         if (GameManager_References._player != null)
    58.         {
    59.             //playerMaster = GameObject.Find("Player").GetComponent<Player_Master>();
    60.             playerMaster = playerController.GetComponent<Player_Master>();
    61.             //playerMaster = GameManager_References._player.GetComponent<Player_Master>();
    62.          
    63.         }
    64.     }
    65. }
    and here is my Player_Master Script:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Player_Master : MonoBehaviour
    7. {
    8.     public delegate void GeneralEventHandler();
    9.     public event GeneralEventHandler EventInventoryChanged;
    10.     public event GeneralEventHandler EventHandsEmpty;
    11.     public event GeneralEventHandler EventAmmoChanged;
    12.  
    13.     public delegate void AmmoPickupEventHandler(string ammoName, int quantity);
    14.     public event AmmoPickupEventHandler EventPickedUpAmmo;
    15.  
    16.     public delegate void PlayerHealthEventHandler(int healthChange);
    17.     public event PlayerHealthEventHandler EventPlayerHealthDeduction;
    18.     public event PlayerHealthEventHandler EventPlayerHealthIncrease;
    19.  
    20.     public void CallEventInventoryChanged()
    21.     {
    22.         if(EventInventoryChanged != null)
    23.         {
    24.             EventInventoryChanged();
    25.         }
    26.     }
    27.  
    28.     public void CallEventHandsEmpty()
    29.     {
    30.         if(EventHandsEmpty != null)
    31.         {
    32.             EventHandsEmpty();
    33.         }
    34.     }
    35.  
    36.     public void CallEventAmmoChanged()
    37.     {
    38.         if(EventAmmoChanged != null)
    39.         {
    40.             EventAmmoChanged();
    41.         }
    42.     }
    43.  
    44.     public void CallEventPickedUpAmmo(string ammoName, int quantity)
    45.     {
    46.         if(EventPickedUpAmmo != null)
    47.         {
    48.             EventPickedUpAmmo(ammoName, quantity);
    49.         }
    50.     }
    51.  
    52.     public void CallEventPlayerHealthDeduction(int dmg)
    53.     {
    54.         if(EventPlayerHealthDeduction != null)
    55.         {
    56.             EventPlayerHealthDeduction(dmg);
    57.         }
    58.     }
    59.  
    60.     public void CallEventPlayerHealthIncrease(int increase)
    61.     {
    62.         if(EventPlayerHealthIncrease != null)
    63.         {
    64.             EventPlayerHealthIncrease(increase);
    65.         }
    66.     }
    67. }
    68.  
    Can u please tell me whats wrong? Need another bit of code? tell me i post u
    i must use public GameObject playerController;
    and playerMaster = playerController.GetComponent<Player_Master>(); in Item Master or else my UI Inventory button dosent reset :|
     
    Last edited: Oct 25, 2017
  27. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    429
    It might be that the player master is not setting up the reference to the player GameObject in time. A script execution order problem. If that is the case try setting up the reference to the player GameObject in Awake in the GameManager_References script.
     
  28. RandomGuardian83

    RandomGuardian83

    Joined:
    Sep 18, 2017
    Posts:
    1
    Hey I'm on episode 98 and I'm getting the null reference exception error on line 41 and I don't know why, the code looks exactly as it should. Thoughts?
    Code (CSharp):
    1. private GunMaster gunMaster;
    2.     private float nextAttack;
    3.     public float attackRate = 0.5f;
    4.     private Transform myTransform;
    5.     public bool isAutomatic;
    6.     public bool hasBurstFire;
    7.     private bool isBurstFireActive;
    8.     public string attackButtonName;
    9.     public string reloadButtonName;
    10.     public string burstFireButtonName;
    11.  
    12.  
    13.  
    14.     void Start () {
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.         CheckIfWeaponShouldAttack ();
    21.         CheckForBurstFireToggle ();
    22.         CheckForReloadRequest ();
    23.     }
    24.  
    25.     void SetInitialReferences()
    26.     {
    27.         gunMaster = GetComponent<GunMaster> ();
    28.         myTransform = transform;
    29.         gunMaster.isGunLoaded = true;  //allows the player to shoot right away
    30.     }
    31.  
    32.     void CheckIfWeaponShouldAttack()
    33.     {
    34.         if (Time.time > nextAttack && Time.timeScale > 0 &&
    35.             myTransform.root.CompareTag(GameManagerReferences._playerTag)) {
    36.             if (isAutomatic && !isBurstFireActive) {
    37.                 if (Input.GetButton (attackButtonName)) {
    38.                     Debug.Log ("Full Auto Fire");
    39.                     AttemptAttack ();
    40.                 }
    41.             } else if (isAutomatic && isBurstFireActive) {
    42.                 if (Input.GetButtonDown (attackButtonName)) {
    43.                     Debug.Log ("Burst Fire Firing");
    44.                     StartCoroutine (RunBurstFire ());
    45.                 }
    46.             }
    47.  
    48.             else if (!isAutomatic) {
    49.                 if (Input.GetButtonDown (attackButtonName)) {
    50.                     AttemptAttack ();
    51.                 }
    52.             }
    53.         }
    54.     }
    55.  
    56.     void AttemptAttack()
    57.     {
    58.         nextAttack = Time.time + attackRate;
    59.         if (gunMaster.isGunLoaded) {
    60.             Debug.Log ("Shooting");
    61.             gunMaster.CallEventPlayerInput ();
    62.         } else {
    63.             gunMaster.CallEventGunNotUsable ();
    64.         }
    65.     }
    66.  
    67.     void CheckForReloadRequest()
    68.     {
    69.         if (Input.GetButtonDown (reloadButtonName) && Time.timeScale > 0 && myTransform.root.CompareTag (GameManagerReferences._playerTag)) {
    70.             gunMaster.CallEventRequestReload ();
    71.             Debug.Log ("player is reloading and cannot fire");
    72.         }
    73.     }
    74.  
    75.     void CheckForBurstFireToggle()
    76.     {
    77.         if (Input.GetButtonDown (burstFireButtonName) && Time.timeScale > 0 && myTransform.root.CompareTag (GameManagerReferences._playerTag)) {
    78.             Debug.Log ("Burst Fire Toggled");
    79.             isBurstFireActive = !isBurstFireActive;
    80.             gunMaster.CallEventToggleBurstFire ();
    81.         }
    82.     }
    83.  
    84.     IEnumerator RunBurstFire()
    85.     {
    86.         AttemptAttack ();
    87.         yield return new WaitForSeconds (attackRate);
    88.         AttemptAttack ();
    89.         yield return new WaitForSeconds (attackRate);
    90.         AttemptAttack ();
    91.     }
    92. }
    93.  
     

    Attached Files:

  29. aidinz

    aidinz

    Joined:
    Apr 17, 2016
    Posts:
    50
    For the FPS tutorial, are you using a pre-made character controller or write one from scratch?

    EDIT: It uses the Standard Asset's character controller.

    I really wished it would teach how to write a 3D character controller.
     
    Last edited: Nov 23, 2017
  30. Nizzio

    Nizzio

    Joined:
    Jan 9, 2016
    Posts:
    1


    Chapter 1 Challenge
     
  31. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    429
    A+ Nicely Done :)
     
  32. scoobz1234

    scoobz1234

    Joined:
    Aug 2, 2016
    Posts:
    4
    Hey All, having a problem once i got to Episode 120, the Destructible_ParticleSpawn script i'm getting null reference for the Initialization method.. i was thinking this may be a script execution error, anyone else having any issue?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace ScoobzGame{
    6.  
    7. public class Destructible_ParticleSpawn : MonoBehaviour {
    8.  
    9.         private Destructible_Master destructibleMaster;
    10.         public GameObject explosionEffect;
    11.  
    12.         void OnEnable () {
    13.             InitRef();
    14.             destructibleMaster.EventDestroyMe += SpawnExplosion;
    15.         }
    16.  
    17.         void OnDisable () {
    18.             destructibleMaster.EventDestroyMe -= SpawnExplosion;
    19.         }
    20.  
    21.         private void Start() {
    22.             InitRef();
    23.             destructibleMaster.EventDestroyMe += SpawnExplosion;
    24.         }
    25.  
    26.         void InitRef(){
    27.             destructibleMaster.GetComponent<Destructible_Master>();
    28.         }
    29.  
    30.         void SpawnExplosion() {
    31.             if (explosionEffect != null) {
    32.                 Instantiate(explosionEffect, transform.position, Quaternion.identity);
    33.             }
    34.         }
    35.     }
    36.  
    37. }
    EDIT: i found that if i make the private Destructible_Master script public, and drag and drop in the script to the field in the inspector, the error goes away...
     
    Last edited: Mar 30, 2018
    KUFgoddess likes this.
  33. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    429
    That's a good solution. I do that quite a bit myself these days to avoid any such issue.
     
    scoobz1234 likes this.
  34. KUFgoddess

    KUFgoddess

    Joined:
    Apr 2, 2015
    Posts:
    23

    I was having those same types of problems a few months ago. I started realizing if I have a null reference that means there isn't a reference, nothing is there or whatever was there is not being called. Now I am able to fix a lot of the different satiations that output a null reference.
     
  35. kingmaker1331

    kingmaker1331

    Joined:
    Apr 10, 2017
    Posts:
    3
    Hey..!Very Awesome tutorials...
    I am Developing my First FPS Game And your tutorials are helping me so much .
    Actually i want to develop it for Mobile Phone.
    i am not much play games on PC.So Can You Or Any One help me for Mobile Inputs.
    Any free kit from unity Assets store.Or how to just put buttons and use them for input.
    Thank you.
     
  36. kingmaker1331

    kingmaker1331

    Joined:
    Apr 10, 2017
    Posts:
    3
    I Also Got This Error But Solved By Rewriting Code of Enemy_Animations. Some times unity Goes Mad.You can try by rewriting code.Look at the problem in which script it cause and rewrite code. Thanks
     
  37. hongwaixuexi

    hongwaixuexi

    Joined:
    Dec 11, 2017
    Posts:
    173
    GTGD:
    It's a very useful tutorial.
    Could you continue on AI FSM? The npc who uses ranged weapon should step back when enemy approach closely, and now they just moving forward.

    Any plan for new tutorial? I hope still on FPS with many details.
     
  38. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    429
    Thanks. The npc goes into the pursue state quite regularly so when they are in the range attack state you could alter the code so they don't go into the pursue state. This will stop them moving towards the enemy. You could alter the range attack state further and borrow a bit from the flee state so they do move back and try to keep away from the enemy.

    It was my intention to create an AI series, because since GTGD S3 I've made my AIs quite a lot more efficient, but Unity has brought in the ECS (Entity Component System - Data Oriented Design) API and it is early days at the moment. ECS to me looks like the future of coding in Unity with MonoBehaviour becoming legacy, but Unity still has a ton of work to do to make ECS as feature rich. For me I'm not keen on making tools, I want to make games so I have to wait till ECS is mature. I would like to make tutorials for ECS and I keep thinking I should, but I really wish Unity made the implementation more complete before releasing it.
     
  39. hongwaixuexi

    hongwaixuexi

    Joined:
    Dec 11, 2017
    Posts:
    173
    Thanks for your advice on adaption. I watched a lot videos and assets, no AI code is as simple as yours while powerful. I also hope you can add some melee fight tutorial like doom4.
     
  40. Lelepoop

    Lelepoop

    Joined:
    Nov 14, 2016
    Posts:
    87
    @GTGD

    I'd like to say thank you because your series from 2012 got the ball rolling for me making FPS games.
    It started out fairly rough in quality, but it has now become this.

    It's not the same code as yours, but this goes to show that with enough research into networking and gameplay anyone can pull off a polished experience.



    BTW, any chance you will be attempting to write a series for the ECS networking engine?
     
  41. sujazoya

    sujazoya

    Joined:
    Jul 10, 2018
    Posts:
    1

    using UnityEngine;
    using UnityEngine.UI;

    namespace S3
    {

    public class Player_HealthPlus : MonoBehaviour
    {
    private Item_Master item_Master;
    private Player_Master player_Master;
    private GameObject plaryerGo;
    public int quntity;
    public bool isTriggerPickup;
    public AudioSource healthAudio;

    void OnEnable()
    {
    SetInitialReferences();
    item_Master.EventObjectPickup += TakeAmmo;

    }

    void OnDisable()
    {
    item_Master.EventObjectPickup -= TakeAmmo;
    }

    // Use this for initialization
    void Start()
    {
    SetInitialReferences();

    }

    void OnTriggerEnter(Collider other)
    {
    if (other.CompareTag(tag: GameManager_References._playerTag) && isTriggerPickup)
    {
    TakeAmmo();
    }
    }

    void SetInitialReferences()
    {
    healthAudio = GetComponent<AudioSource>();
    item_Master = GetComponent<Item_Master>();
    plaryerGo = GameManager_References._player;

    if (isTriggerPickup)
    {
    if (GetComponent<Collider>() != null)
    {
    GetComponent<Collider>().isTrigger = true;
    }
    if (GetComponent<Rigidbody>() != null)
    {
    GetComponent<Rigidbody>().isKinematic = true;
    }

    }
    }
    void TakeAmmo()
    {
    plaryerGo.GetComponent<Player_Master>().CallEventEventPlayerHealthInrease(quntity);
    healthAudio.Play();
    Destroy(gameObject);
    }
    }
    }
     
  42. KenjiJU

    KenjiJU

    Joined:
    Dec 31, 2012
    Posts:
    6
    Hello.

    I'm getting NullReferenceException when picking up or throwing objects. I can pick them up, but they don't throw properly. They were previously throwing properly until I started working on the weapon section.

    I'd appreciate some help because this has had me stumped, so I haven't touched the project in months.

    The code fails at lines 24 and 34.

    Code (CSharp):
    1. public class ItemMaster : MonoBehaviour {
    2.  
    3.     private PlayerMaster playerMaster;
    4.     private GameManager_References references;
    5.  
    6.     public delegate void GeneralEventHandler();
    7.     public event GeneralEventHandler EventObjectThrow;
    8.     public event GeneralEventHandler EventObjectPickup;
    9.  
    10.     public delegate void PickupActionEventHandler(Transform item);
    11.     public event PickupActionEventHandler EventPickupAction;
    12.  
    13.     void Start() // changed from OnEnable
    14.     {
    15.         SetInitialReferences();
    16.     }
    17.  
    18.     public void CallEventObjectThrow()
    19.     {
    20.         if(EventObjectThrow != null)
    21.         {
    22.             EventObjectThrow();
    23.         }
    24.         playerMaster.CallEventHandsEmpty();
    25.         playerMaster.CallEventInventoryChanged();
    26.     }
    27.  
    28.     public void CallEventObjectPickup()
    29.     {
    30.         if(EventObjectPickup != null)
    31.         {
    32.             EventObjectPickup();
    33.         }
    34.         playerMaster.CallEventInventoryChanged();
    35.     }
    36.  
    37.     public void CallEventPickupAction(Transform item)
    38.     {
    39.         if(EventPickupAction != null)
    40.         {
    41.             EventPickupAction(item);
    42.         }
    43.     }
    44.  
    45.     void SetInitialReferences()
    46.     {
    47.         if(GameManager_References._player != null)
    48.         {
    49.             references = GetComponent<GameManager_References>();
    50.             playerMaster = GameManager_References._player.GetComponent<PlayerMaster>();
    51.         }
    52.     }
    53. }