Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Start timer for a mutiplayer shooter game when player is instanciated

Discussion in 'Scripting' started by einnerlink, Jun 21, 2023.

  1. einnerlink

    einnerlink

    Joined:
    Mar 17, 2022
    Posts:
    3
    Sure, I can help you correct that paragraph. Here is the corrected paragraph:

    Hi everyone. I'm new to developing games with Unity, and I'm not very good at programming yet. However, I'm working on a multiplayer shooter game, and I need to start a timer when the player or players are instantiated. I don't understand why what I've done so far isn't working.

    Here's what I have so far: When the player is instantiated (by the PlayerManager script), the InitGame function is initialized, which turns the initTime boolean variable to true. This should start the timer, but it doesn't.

    I'm hoping someone can help me figure out why the timer isn't starting. Thanks in advance!

    script GameManager

    Code (CSharp):
    1. using Photon.Pun;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class PlayerManager : MonoBehaviour
    8. {
    9.     public PhotonView photonview;
    10.     GameManager manager;
    11.  
    12.     [Header("Player setting")]
    13.     public CharacterController character;
    14.     public PlayerMovement playerMove;
    15.     public GameObject mainCamera;
    16.     public AudioListener audioListener;
    17.     public LookWithMouse lookWith;
    18.     public MeshRenderer mainMesh;
    19.     public TextMeshPro mainNickName;
    20.     //public AnimationController anim;
    21.  
    22.     private void Start()
    23.     {
    24.         manager = GameObject.Find("Manager").GetComponent<GameManager>();
    25.  
    26.         if (PhotonNetwork.IsMasterClient && photonview.IsMine)// soy el cliente maestro
    27.         {
    28.             manager.Insta_Enemy(10);
    29.         }
    30.         if(PhotonNetwork.CountOfPlayers >= 2)
    31.         {
    32.             GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
    33.             for (int i = 0; i < players.Length; i++)
    34.             {
    35.                 players[i].GetComponent<PhotonView>().RPC("InitGame", RpcTarget.All);
    36.             }
    37.         }
    38.     }
    39.    
    40.     [PunRPC]
    41.     public void InitGame()
    42.     {
    43.         manager.InitGame();
    44.         manager.waitPlayers.SetActive(false);
    45.     }
    46.     private void Update()
    47.     {
    48.         if(!photonview.IsMine)
    49.         {
    50.             return;
    51.         }
    52.     }
    53. }
    54.  
    And PlayerManager script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using UnityEngine.AI;
    6. using UnityEngine.UI;
    7. using TMPro;
    8. using System;
    9.  
    10. public class GameManager : MonoBehaviour
    11. {
    12.     public Transform[] enemiSpwn;
    13.  
    14.     [Header("Timer")]
    15.     public GameObject waitPlayers;
    16.     bool initTime;
    17.     public Text textTimer;
    18.     float sub;
    19.     TimeSpan timer;
    20.  
    21.     public void Insta_Enemy(int countEnemy)
    22.     {
    23.         StartCoroutine(InstaEnemi(countEnemy));
    24.     }
    25.  
    26.     IEnumerator InstaEnemi(int count)
    27.     {
    28.         for (int i = 0; i < count; i++)
    29.         {
    30.             int ran = UnityEngine.Random.Range(0, enemiSpwn.Length);
    31.             GameObject newEnemi = PhotonNetwork.Instantiate("Enemi", enemiSpwn[ran].position, enemiSpwn[ran].rotation);
    32.             if (PhotonNetwork.IsMasterClient)
    33.             {
    34.                 newEnemi.GetComponent<NavMeshAgent>().enabled = true;
    35.                 newEnemi.GetComponent<EnemyController>().enabled = true;
    36.             }
    37.             yield return new WaitForSeconds(UnityEngine.Random.Range(5, 50));
    38.         }
    39.     }
    40.     public void InitGame()
    41.     {
    42.         initTime = true;
    43.     }
    44.     private void Update()
    45.     {
    46.         if(initTime && sub < 300)
    47.         {
    48.             sub += Time.deltaTime;
    49.             timer = new TimeSpan(0, 5, 0);
    50.             TimeSpan deltaTimeSpan = TimeSpan.FromSeconds(sub);
    51.             timer = timer.Subtract(deltaTimeSpan);
    52.             //string t = "Time left: " + timer.ToString("hh\\:mm\\:ss");
    53.             textTimer.text = timer.ToString("hh\\:mm\\:ss");
    54.         }
    55.         else
    56.         {
    57.             //
    58.         }
    59.     }
    60. }
    61.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    First of all, don't do crazy ninja coding techniques like this.

    Remember the first rule of GameObject.Find():

    Do not use GameObject.Find();

    More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

    More information: https://forum.unity.com/threads/why-cant-i-find-the-other-objects.1360192/#post-8581066

    In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

    If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way(tm) success of accessing things in your game.


    Next, once you have eliminated crazy ninja code techniques that are already known to fail, if your code does not yet work, then...

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  3. einnerlink

    einnerlink

    Joined:
    Mar 17, 2022
    Posts:
    3
    Well, I just got the answer! In GameManager,
    Code (CSharp):
    1.  if(PhotonNetwork.CountOfPlayers >= 2)
    line 30. I was just testing myself and never was goint to start the timer, so I changed to 1 and that's it!

    Code (CSharp):
    1. if(PhotonNetwork.CountOfPlayers >= 1)
    if(PhotonNetwork.CountOfPlayers >= 1)