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 gimoj · Apr 21, 2014 at 04:07 PM · colliderspawnnullreferenceexceptionweaponspawner

Getting NullReferenceException from the enemies my spawner makes and it won't collide with my weapon.

I'm getting the NullReferenceException error in my game from the enemies that I spawn using my spawner. I have some enemies on the stage that work fine but when they are all destroyed the error occurs with the new batch where my weapon doesn't collide with them anymore because it can't find the gameobject.

This is what I used for my spawner: public class bugSpawner : MonoBehaviour {

 public float spawnTime = 5f;        // The amount of time between each spawn.
 public float spawnDelay = 3f;        // The amount of time before spawning starts.
 public GameObject[] enemies;        // Array of enemy prefabs.
 private Vector3 startPosition;
 public GameObject enemybug;
 
 
 void Start ()
 {

// // Start calling the Spawn function repeatedly after a delay .

     InvokeRepeating("Spawn", spawnDelay, spawnTime);
 }
 
 
 void Spawn ()
 {
     startPosition = transform.position;

     GameObject bugspawn = Instantiate(enemybug) as GameObject;

     bugspawn.transform.position = transform.position;

}

}

And this is for my weapon

 void OnTriggerEnter2D (Collider2D col) 
 {
     GameObject Bugbot = GameObject.Find ("bugspawn");
     Transform BugbotTransform = Bugbot.transform;
     // get player position
     Vector3 bugbotxposition = BugbotTransform.position;

     // If it hits an enemy...
     if (col.tag == "Enemy") {

         col.gameObject.GetComponent<Bugbot>().HP -= 2;

         if (gameObject.transform.position.x > bugbotxposition.x){
             col.gameObject.GetComponent<Bugbot>().bumpRight();
             }
         if (gameObject.transform.position.x < bugbotxposition.x){
             col.gameObject.GetComponent<Bugbot>().bumpLeft();
             }    
         }

}

Edit:

I get this error everytime NullReferenceException: Object reference not set to an instance of an object Swatter.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/Scripts/Swatter.cs:19)

line 19 is:

Transform BugbotTransform = Bugbot.transform;

Comment
Add comment · Show 3
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 robertbu · Apr 21, 2014 at 04:08 PM 1
Share

Please edit your question and paste in a copy of the error message from the console. Also indicate in your question what line is generating the error.

avatar image gimoj · Apr 23, 2014 at 09:07 AM 0
Share

Oh sorry, new at this. Here's the weird part: I have a bugbot in the scene when the game starts and while that bugbot is present all the others work fine and I can destroyed them and the script finds all the bugbots being spawned. But when the first bugbot, the one present at the start, gets destroyed all the bugbots now return a nullreferenceexception. I did the same as the 2d tutorial did but for some reason it's not finding the gameobjects that I spawn.

avatar image KiraSensei · Apr 23, 2014 at 12:37 PM 0
Share

Does your bugbot has "bugspawn" as name ?

0 Replies

· Add your reply
  • Sort: 

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

21 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

Related Questions

NullReferenceException by trying to access another script. 0 Answers

Trigger Spawning? 1 Answer

How to null-reference check a Collider2D that's been destroyed 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

NullReferenceException question. 2 Answers


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