Unity3d - kłopot z kamerą

0

Mam nowy problem z metodą FindGameObjectWithTag. Błąd w VS brzmi następująco:

Severity Code Description Project File Line Suppression State
Error NullReferenceException: Object reference not set to an instance of an object Solution 'Shape.io' ‎(1 project) C:/Users/hubot/Documents/Shape.io/Assets/Scripts/PlayerMovement.cs 28

i w Unity3d:

NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.Update () (at C:/Users/hubot/Documents/Shape.io/Assets/Scripts/PlayerMovement.cs:28)

Kod:

    using System;
    using UnityEngine;
    using UnityEngine.Networking;
    using UnityEngine.SceneManagement;
     
    public class PlayerMovement : NetworkBehaviour
    {
        // Use this for initialization
        void Start()
        {
            player = GameObject.FindGameObjectWithTag("Player");
            camera = GameObject.FindGameObjectWithTag("MainCamera");
        }
       
        // Update is called once per frame
        void Update()
        {
            if (!isLocalPlayer)
            {
                return;
            }
     
            float speed = 7.0f;
           
            var move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
            transform.position += move * speed * Time.deltaTime;
     
            camera.transform.position =
                new Vector3(player.gameObject.transform.position.x,
                            player.gameObject.transform.position.y,
                            cameraDistance);
     
            if (transform.localScale.x <= 0
                || transform.localScale.y <= 0
                || transform.localScale.z <= 0)
            {
                Destroy(gameObject);
                Network.Disconnect();
                MasterServer.UnregisterHost();
                SceneManager.LoadScene("Offline");
            }
        }
     
        void OnTriggerEnter(Collider other)
        {
            if (other.CompareTag("Bullet"))
            {
                Destroy(other.gameObject);
                transform.localScale += new Vector3(0.1f, 0.1f, 0.1f);
                if (Convert.ToInt32(transform.localScale.x) % 8 == 0
                    && Convert.ToInt32(transform.localScale.y) % 8 == 0
                    && Convert.ToInt32(transform.localScale.z) % 8 == 0)
                {
                    cameraDistance -= 10;
                }
            }
           
            if (other.CompareTag("Boundary"))
            {
                Destroy(gameObject);
                Network.Disconnect();
                MasterServer.UnregisterHost();
                SceneManager.LoadScene("Offline");
            }
     
            if (other.CompareTag("Player"))
            {
                transform.localScale -= new Vector3(1.0f, 1.0f, 1.0f);
                other.transform.localScale += new Vector3(1.0f, 1.0f, 1.0f);
            }
        }
     
        private GameObject player;
        private new GameObject camera;
        private int cameraDistance = -10;
    }
     

i zrzuty ekranu:

Wideo z grą na żywo: https://1drv.ms/v/s!Av9IyCaZBquemHTEzrzvBDl4Bi_N.
Kamera powinna przesuwać się razem z graczem.
Może ktoś mi pomóc? Z góry dziękuję.

0

Czemu masz zmienne na samym dole kodu???

0

a masz obiekt otagowany jako player?

Blad jest taki, ze nie GameObject player jest nullem (wiec go nie znajduje)

Szukaj obiektow poprzez

this.transform.Find("nazwaObiektu")

https://docs.unity3d.com/ScriptReference/Transform.Find.html

tylko pamietaj ze transform bedzie szukal od rodzica w dol, jezeli chcesz na calej hierarchi szukac to uzyj

GameObject.Find("nazwaObiektu");

ale ta operacja jest kosztowna

1 użytkowników online, w tym zalogowanych: 0, gości: 1