

I have nemySpawner scriptI: private void SpawnEnemy() { //if (enemyAsset != null) // if enemyAsset array is not null if (enemyAsset.Length > 0) { GameObject enemyPrefab2Spawn = enemyAsset[0]; GameObject enemy = Instantiate(enemyPrefab2Spawn, LvlManager.Instance.startPos.position, Quaternion.identity); enemy.GetComponent ().targetPos = LvlManager.Instance.startPos; } else { Debug.Log("enemyAsset array is void!"); } } using this script enemies can be spawned using System.Collections; using UnityEngine; public class movement : MonoBehaviour { public Rigidbody2D rb; // //[SerializeField] public LevelManager levlManager; public float movementSpeed; private float originalSpeed; [SerializeField] public Transform targetPos; private int pathIdx = 0; private void Awake() { targetPos = LvlManager.Instance.startPos; } private void Start() { /* if (LvlManager.path == null) { UnityEngine.Debug.Log("LvlManager.path è NULL "); } else */ targetPos = LvlManager.path[pathIdx]; originalSpeed = movementSpeed; } private void Update() { if (LvlManager.totalEnergy > 0) { if (Vector2.Distance(targetPos.position, transform.position) <><><> gameRunning is:"); UnityEngine.Debug.Log(LvlManager.Instance.gameRunning); } private void FixedUpdate() { Vector2 direction = (targetPos.position - transform.position).normalized; if (direction == null) { UnityEngine.Debug.Log("<>uuuu <> direction è NULL "); } if (LvlManager.Instance.gameRunning == true) { UnityEngine.Debug.Log("gameRunning is TRUE!"); rb.linearVelocity = direction * movementSpeed; } else { UnityEngine.Debug.Log("gameRunning is FALSE!"); // Stop movement rb.linearVelocity = Vector3.zero; // Stop rotation rb.angularVelocity = 0.0f; } } public void decreaseVelovity(float velDrecrease) { // drease movementSpeed by velDrecrease . must be always > 0 // movementSpeed -= velDrecrease; movementSpeed = Mathf.Max(0.3f, movementSpeed - velDrecrease); // drease movementSpeed by velDrecrease . must be at least 0.3 StartCoroutine(RestoreSpeed()); } private IEnumerator RestoreSpeed() { yield return new WaitForSeconds(4f); UnityEngine.Debug.Log("velocita ripristinata !"); movementSpeed = originalSpeed; } } attached to each enemy is attached also the movement script. i cannot cope with reason why am I getting whis error: NullReferenceException: Object reference not set to an instance of an object movement.Update () (at Assets/Scripts/movement.cs:51) I hope having provided enough information 1 post - 1 participant Read full topic
Unity Discussions
discussions.unity.com