Quantcast
Viewing latest article 2
Browse Latest Browse All 8

NullReferenceException: Object reference not set to an instance of an object.

Hello, I'm having trouble understanding why Unity is giving me the above error. Here is my code: Camera cam; //Code outside of functions should stick to declaring types or specific values only, so don't code "cam=Camera.main here" float spawnPos; float xCoordinate; float yCoordinate; public bool respawn; Transform spawnPoint; Transform mouse; mouseMovement mouseMovement; Rigidbody2D clone; void Awake(){ spawnPoint = GameObject.Find ("spawnPoint").transform; mouse = GameObject.FindWithTag ("Mouse").transform; mouseMovement = GetComponent(); cam = Camera.main; respawn = true; if (spawnPoint != null) { Debug.Log ("Yay, you found it."); } else { Debug.Log ("Where is it?"); } } void FixedUpdate () { Vector2 position = cam.ScreenToWorldPoint(new Vector2(Screen.width, Random.Range(Screen.height,0))); spawnPoint.position = position; //move the spawnPoint at random ranges of the width of the camera //spawn a mouse after three seconds StartCoroutine (delay ()); respawn = false; //this is to make sure the mouse spawns only once per 3 seconds } IEnumerator delay(){ if (respawn==true){ clone = Instantiate(mouse, spawnPoint.position, Quaternion.identity)as Rigidbody2D; Debug.Log ("hi"); clone.velocity = new Vector2 (0, mouseMovement.velocity); } yield return new WaitForSeconds (3); respawn=true; } } The error occurs on line 49: clone.velocity = new Vector2 (0, mouseMovement.velocity); There could be some newbie mistakes as I am still learning. Please bear with me and thank you!

Viewing latest article 2
Browse Latest Browse All 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>