Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

How do I get the clicked object.

Discussion in 'Netcode for GameObjects' started by Danisanca, Jun 9, 2021.

  1. Danisanca

    Danisanca

    Joined:
    Nov 14, 2020
    Posts:
    3
    I'm implementing mlapi in my game and I don't know much about multiplayer. Could someone tell me why I'm in error in this code.
    ------------------
    Erro:
    NullReferenceException: Could not get NetworkObject for the NetworkBehaviour. Are you missing a NetworkObject component?
    MLAPI.NetworkBehaviour.get_NetworkObject () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkBehaviour.cs:282)
    MLAPI.NetworkBehaviour.get_IsLocalPlayer () (at Library/PackageCache/com.unity.multiplayer.mlapi@3e3aef6aa0/Runtime/Core/NetworkBehaviour.cs:239)
    Target.Update () (at Assets/Componentes/Target/Target.cs:36)
    -------------------


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using MLAPI;
    6. using MLAPI.Messaging;
    7. using MLAPI.NetworkVariable;
    8.  
    9. public class Target : NetworkBehaviour
    10. {
    11.     public CanvasTarget CvTarget;
    12.  
    13.     public GameObject prefplayer;
    14.     public Personagem player;
    15.     Vector3 touchPosWorld;
    16.     TouchPhase touchPhase = TouchPhase.Ended;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         if (IsLocalPlayer)
    21.         {
    22.             CvTarget = GetComponent<CanvasTarget>();
    23.         }
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         if (prefplayer == null)
    30.         {
    31.             prefplayer = GameObject.FindGameObjectWithTag("DataBase").GetComponent<GamesObjectsIns>().PrefPerso;
    32.             player = prefplayer.GetComponentInParent<Personagem>();
    33.         }
    34.  
    35.         //toutchscreen();
    36.         if (IsLocalPlayer)
    37.         {
    38.             touchMouse();
    39.         }
    40.  
    41.        
    42.  
    43.     }
    44.  
    45.  
    46.     public void toutchscreen()
    47.     {
    48.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == touchPhase)
    49.         {
    50.             touchPosWorld = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    51.  
    52.             Vector2 touchPosWorld2D = new Vector2(touchPosWorld.x, touchPosWorld.y);
    53.  
    54.             RaycastHit2D hitInformation = Physics2D.Raycast(touchPosWorld2D, Camera.main.transform.forward);
    55.  
    56.             if (hitInformation.collider != null)
    57.             {
    58.                 GameObject touchedObject = hitInformation.transform.gameObject;
    59.                 if (touchedObject.GetComponent<Monstro>())
    60.                 {
    61.                     CvTarget.GetComponent<CanvasTarget>().setTarget(touchedObject.transform.gameObject);
    62.                 }
    63.                 else if (touchedObject.GetComponent<Personagem>().ID != player.ID)
    64.                 {
    65.                     CvTarget.GetComponent<CanvasTarget>().setTarget(touchedObject.transform.gameObject);
    66.                 }
    67.             }
    68.  
    69.  
    70.         }
    71.     }
    72.    
    73.  
    74.     public void touchMouse()
    75.     {
    76.      
    77.  
    78.         if (Input.GetMouseButtonDown(0))
    79.         {
    80.          
    81.                 Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    82.                 Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
    83.                 RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
    84.            
    85.          
    86.             if (hit.collider != null)
    87.             {
    88.                 if (hit.collider.GetComponent<Personagem>())
    89.                 {
    90.                     if (hit.collider.GetComponent<Personagem>().ID != player.ID)
    91.                     {
    92.                         CvTarget.GetComponent<CanvasTarget>().setTarget(hit.collider.gameObject);
    93.  
    94.                     }
    95.  
    96.  
    97.  
    98.  
    99.                 }
    100.                 if (hit.collider.GetComponent<Monstro>())
    101.                 {
    102.                     CvTarget.GetComponent<CanvasTarget>().setTarget(hit.collider.GetComponent<Monstro>().gameObject);
    103.                 }
    104.  
    105.             }
    106.         }
    107.     }
    108.  
    109. }
    110.  
    111.  
     
  2. MadMojo

    MadMojo

    Joined:
    Oct 16, 2014
    Posts:
    18
    Probably forgot to add the NetworkObject component to the game object.
     
    Danisanca likes this.
  3. Danisanca

    Danisanca

    Joined:
    Nov 14, 2020
    Posts:
    3
    It stopped giving an error, but I still can't click to select the monster on the screen. Apparently it is not recognizing the click because it does not return anything in Debug.log.
     
unityunity