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 A simple question for experts about isLocalPlayer.

Discussion in 'Scripting' started by uotsabchakma, Sep 20, 2020.

  1. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Guys I have a problem with this code below. Unity says GameObject does not contain
    Code (CSharp):
    1. isLocalPlayer
    . But I do have used isLocalPlayer on player object. I anyway want to get isLocalPlayer from player variable game object anyway. How to do that? If you can so please upload the full code for getting isLocalPlayer. This way, you'll not need to explain me how the code works, because just by watching the code I'll undeerstand how'll the code will work.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Mirror.Examples.Pong
    4. {
    5.     public class localMeshInvisibility : MonoBehaviour
    6.     {
    7.         public GameObject player;
    8.  
    9.         void Start()
    10.         {
    11.             player = GameObject.Find("Player");
    12.             if (player.isLocalPlayer)
    13.             {
    14.                 SetActive = false;
    15.             }
    16.         }
    17.     }
    18. }
    Thanks to the persons who had explained or writed the code for me about how to get the isLocalPlayer bool in this code.
     
  2. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    You need to learn how to search for answers yourself instead of relying too much on others for trivial things. I never even used Mirror but simply searching for 'mirror islocalplayer' on google gave me this page as the first result https://mirror-networking.com/docs/Articles/Guides/GameObjects/SpawnPlayer.html

    Now to your issue. Your script derives from MonoBehaviour while it should derive from NetworkBehaviour. The isLocalPlayer is a member of the NetworkBehaviour class and not the GameObject class. Go read the page.
     
    B1n1_ and Joe-Censored like this.
  3. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Oh, you're right, I'm using Monobehaviour. Thanks for a silution. I'll try it out. Then if it not works, Sorry, I'll need to disturb you again, if it not work sorry.
     
  4. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    No, it isn't working yet. Unity yet says, the class GameObject dosen't contains isLocalPlayer. Here's the new code with NetworkBehaviour. The player gameObject too runs with NetworkBehaviour.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace Mirror.Examples.Pong
    6. {
    7.     public class localMeshInvisibility : NetworkBehaviour
    8.     {
    9.         public GameObject player;
    10.  
    11.         void Start()
    12.         {
    13.             player = GameObject.Find("Player");
    14.             if (player.isLocalPlayer)
    15.             {
    16.                 SetActive = false;
    17.             }
    18.         }
    19.     }
    20. }
    21.  
     
  5. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Okay, I used my hard brain and found this works:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace Mirror.Examples.Pong
    6. {
    7.     public class localMeshInvisibility : NetworkBehaviour
    8.     {
    9.         public GameObject player;
    10.  
    11.         player_movement playerMovement;
    12.  
    13.         bool playerIsLocal = false;
    14.  
    15.         void Start()
    16.         {
    17.             player = GameObject.Find("Player");
    18.             playerMovement = player.GetComponent<player_movement>();
    19.  
    20.             if (playerMovement.isLocalPlayer)
    21.             {
    22.                 SetActive(false);
    23.             }
    24.         }
    25.     }
    26. }
    27.  
    Now I don't know how 'SetActive' isn't working. It says, "Assets\Scripts\localMeshInvisibility.cs(22,17): error CS0103: The name 'SetActive' does not exist in the current context" What is Unity meaning by context? The 'NetworkBehaviour'? Can you tell me? If you know how to solve this please tell me!
     
  6. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Okay, I solved that too. No answer about that is not needed now. I was actually trying to use SetActive with mesh.
    But another problem. I sometimes fell into this but never asked anyone about how to solve it. Unity is now saying "
    NullReferenceException: Object reference not set to an instance of an object
    Mirror.Examples.Pong.player_movement.Update () (at Assets/Scripts/player_movement.cs:45)" What that means, how to solve it?
     
  7. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    NullReferenceException means that the thing you're trying to access/modify isn't set. It can be a script or a variable, and eventhough it might exist, your reference to it isn't pointing to anything.
     
    uotsabchakma likes this.
  8. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    You're right, I was really missing playerController to assign. Now everything is working as I expected. Thanks for helping me!