Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by karlojeda · Aug 11, 2020 at 03:47 AM · missingreferenceexception

MissingReferenceException:

So I'm doing Brackys how to make a TD Game Here's the video link

here's my code

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class EnemyMovement : MonoBehaviour
 {
     public float speed = 1f;
 
     private Transform target;
     private int wavepointIndex = 0;
 
     void Start()    
     {
         target = Waypoints.waypoints[0];
 
     }
 
     void Update()
     {
         Vector3 dir = target.position - transform.position;
         transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
 
         if(Vector3.Distance(transform.position, target.position) <= .2f)
         {
             getNextWaypoint();
         }
 
 
     }
 
 
     void getNextWaypoint()
     {
         if(wavepointIndex >= Waypoints.waypoints.Length - 1)
         {
             Destroy(gameObject);
             return;
         }
 
         wavepointIndex++;
         target = Waypoints.waypoints[wavepointIndex];
     }
 }
 


MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object

I don't know what's wrong with it. and here is the whole error.

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.



Your script should either check if it is null or you should not destroy the object. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, UnityEngine.Vector3 pos, UnityEngine.Quaternion rot) (at :0) UnityEngine.Object.Instantiate (UnityEngine.Object original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at :0) UnityEngine.Object.Instantiate[T] (T original, UnityEngine.Vector3 position, UnityEngine.Quaternion rotation) (at :0) WaveSpawner.SpawnEnemy () (at Assets/WaveSpawner.cs:54) WaveSpawner+d__7.MoveNext () (at Assets/WaveSpawner.cs:44) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at :0)





Then here's the code that's it's pointing to

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class WaveSpawner : MonoBehaviour
 {
 
     public Transform enemyPrefab;
     public Transform spawnPoint;
 
 
     public float timeBetweenWaves = 5f;
     private float countdown = 2f;
 
     private int waveIndex = 0;
 
     public Text waveCountdownText;
 
 
     void Update()
     {
         if(countdown <= 0f)
         {
             StartCoroutine(SpawnWave());
             countdown = timeBetweenWaves;
 
         }
 
         countdown -= Time.deltaTime;
 
         waveCountdownText.text = Mathf.Floor(countdown).ToString();
 
 
 
 
     }
 
     IEnumerator SpawnWave()
     {
         waveIndex++;
         for (int i = 0; i < waveIndex; i++)
         {
             SpawnEnemy();
             yield return new WaitForSeconds(.5f);
         }
        
     }
 
     void SpawnEnemy()
     {
             Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
     }
 }
 




Heading

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UnityedWeStand · Aug 11, 2020 at 04:14 AM 0
Share

what is being plugged into spawnPoint?

avatar image karlojeda UnityedWeStand · Aug 11, 2020 at 04:46 AM 0
Share

Never$$anonymous$$d I got it. Thank you for that idea.

For the enemy prefab - i was plugging in a prefab in the hierarchy. not the one from the project folder prefab.

so i deleted the one from the hierarchy. then plugged the enemy prefab to the wave spawner and it is now working without errors.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by karlojeda · Aug 11, 2020 at 04:47 AM

Solved:

For the enemy prefab - i was plugging in a prefab in the hierarchy. not the one from the project folder prefab.

so i deleted the one from the hierarchy. then plugged the enemy prefab to the wave spawner and it is now working without errors.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by IllegalRazer · Aug 12, 2020 at 09:22 AM

Im guessing that u assigned a gameObject with the Enemy movement script to the spawnPoint variable in the waveSpawner.

However, in the EnemyMovevemt Script, the script destroys the gameObject the script is linked to in line 37. This causes the reference to the gameObject of spawnPoint to be null. In waveSpawner, you tried to access the position and rotation of spawnPoint. However, the gameObject assigned to spawnPoint has been destroyed. This causes your script to try to access null, causing the error.

It is very unusual for a script to destroy its linked gameObject, so I would suggest modifying line 37 in EnemyMovement.,Im guessing that u assigned a gameObject with the Enemy movement script to the spawnPoint variable in the waveSpawner.

However, in the EnemyMovevemt Script, the script destroys the gameObject the script is linked to in line 37. This causes the reference to the gameObject of spawnPoint to be null. In waveSpawner, you tried to access the position and rotation of spawnPoint. However, the gameObject assigned to spawnPoint has been destroyed. This causes your script to try to access null, causing the error.

It is very unusual for a script to destroy its linked gameObject, so I would suggest modifying line 37 in EnemyMovement.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by IllegalRazer · Aug 11, 2020 at 04:45 AM

Im guessing that u assigned a gameObject with the Enemy movement script to the spawnPoint variable in the waveSpawner.

However, in the EnemyMovevemt Script, the script destroys the gameObject the script is linked to in line 37. This causes the reference to the gameObject of spawnPoint to be null. In waveSpawner, you tried to access the position and rotation of spawnPoint. However, the gameObject assigned to spawnPoint has been destroyed. This causes your script to try to access null, causing the error.

It is very unusual for a script to destroy its linked gameObject, so I would suggest modifying line 37 in EnemyMovement.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

133 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MissingReferenceException Help 1 Answer

DontDestroyOnLoad error 2 Answers

MissingReferenceException Help Please! 0 Answers

Why Player input aways get MissingReferenceException? 0 Answers

Odd collision behaviour with If/while statement 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges