Hi everyone! I'm trying to learn some C# from some tutorials to make games with Unity. I've found a game tutorial but a problem stuck me and I can't fix it, I'm not fully mastered in programming to solve by myself so I'm here to ask for your help. I've follow this tutorial from A to Z but probably some informations are missing, I'm stopped on 48:45 after write these codes to make the shoot. Also, the ball is stuck on the floor. Before them, the ball moved like in the video. Thanks in advance to those who will take the time to read and help me. The errors are these: - NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/Game/Player.cs:54) Code (CSharp): using StarterAssets; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { private StarterAssetsInputs starterAssetsInputs; private Animator animator; private Ball ballAttachedToPlayer; private float timeShot = -1f; public const int ANIMATION_LAYER_SHOOT = 1; public Ball BallAttachedToPlayer { get => ballAttachedToPlayer; set => ballAttachedToPlayer = value; } // Start is called before the first frame update void Start() { starterAssetsInputs = GetComponent<StarterAssetsInputs>(); } // Update is called once per frame void Update() { if (starterAssetsInputs.shoot) { starterAssetsInputs.shoot = false; timeShot = Time.time; animator.Play("Shoot", ANIMATION_LAYER_SHOOT, 0f); animator.SetLayerWeight(ANIMATION_LAYER_SHOOT, 1F); } if (timeShot > 0) { //shoot ball if (ballAttachedToPlayer != null && Time.time - timeShot > 0.2) { ballAttachedToPlayer.StickToPlayer = false; Rigidbody rigidbody = ballAttachedToPlayer.transform.gameObject.GetComponent<Rigidbody>(); rigidbody.AddForce(transform.forward * 20f, ForceMode.Impulse); ballAttachedToPlayer = null; } // finish kicking animation if (Time.time - timeShot > 0.5) { timeShot = -1f; } } else { animator.SetLayerWeight(ANIMATION_LAYER_SHOOT, Mathf.Lerp(animator.GetLayerWeight(ANIMATION_LAYER_SHOOT), 0f, Time.deltaTime * 10f)); } } } - NullReferenceException: Object reference not set to an instance of an object Ball.Update () (at Assets/Ball.cs:41) Code (CSharp): using StarterAssets; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; public class Ball : MonoBehaviour { [SerializeField] private Transform transformPlayer; [SerializeField] private Transform playerBallPosition; private bool stickToPlayer; float speed; Vector3 previousLocation; Player scriptPlayer; public bool StickToPlayer { get => stickToPlayer; set => stickToPlayer = value; } // Start is called before the first frame update void Start() { playerBallPosition = transformPlayer.Find("Geometry").Find("BallLocation"); scriptPlayer = transformPlayer.GetComponent<Player>(); } // Update is called once per frame void Update() { if (!stickToPlayer) { float distanceToPlayer = Vector3.Distance(transformPlayer.position, transform.position); if (distanceToPlayer < 0.5) { StickToPlayer = true; scriptPlayer.BallAttachedToPlayer = this; } } else { Vector2 currentLocation = new Vector2(transform.position.x, transform.position.z); speed = Vector2.Distance(currentLocation, previousLocation) / Time.deltaTime; transform.position = playerBallPosition.position; transform.Rotate(new Vector3(transformPlayer.right.x, 0, transformPlayer.right.z), speed, Space.World); previousLocation = currentLocation; } } }
It's not C++, it's C#. There is a sticky that explains how to fix null reference exceptions, since it is a common error. https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
I made a typing mistake! Thanks for pointing it out and thanks for the link thread, I'll give it a look. Hope I can fix it! Thank you again!