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 Apinaheebo · Feb 12, 2013 at 01:18 PM · gameobjectplayeractive

Making the enemy shoot only when player is alive

I'm making a pretty simple bullethell game, and I would like the enemy to stop shooting when my player dies(it is active when alive, and not active when dead). I don't know where I fail in the code. It gives me a NullReferenceException when my player dies (gets deactivated).

var enemyLaser : GameObject; var fireFreq : float; var projectileSpeed:float = 0.0; private var lastShot : float;

 function Update () {
     if (Time.time > lastShot + fireFreq){
         Fire();
         }
 }
 
 function Fire() {
     lastShot = Time.time;
     var player = GameObject.Find("player");
         if (player.active){
                     var shoot = Instantiate(enemyLaser,transform.position,transform.rotation);
             shoot.rigidbody.velocity = (player.transform.position - transform.position).normalized * projectileSpeed;
             }
 }

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 Landern · Feb 12, 2013 at 01:38 PM 0
Share

Well... What line is the null reference co$$anonymous$$g from? is it when you attempt to file the GameObject 'player'?

Have you not attached a prefab to enemyLaser in the inspector?

avatar image Apinaheebo · Feb 12, 2013 at 01:49 PM 0
Share

It comes from Line 15. The game seems to work fine, until they hit me and my character dies. NullReferenceException comes then. The script does exactly what I want until that point (they will shoot at the player's position). And yes, I have attached the prefab.

Without that if statement, the game is completely playable, but I want them to stop shooting while I'm respawning.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dave-Carlile · Feb 12, 2013 at 02:22 PM

If you're destroying or disabling the player when they die, then GameObject.Find wil no longer find that object. So after this line...

 var player = GameObject.Find("player");

The player variable will be null, then when you reference the player.active property...

         if (player.active)

you'll get a null reference exception. You can fix it by changing your if statement...

 if (player != null && player.active)
 {
    ... your stuff here ...
 }


So it will only execute the code inside if when the player object is found.

Comment
Add comment · Show 1 · 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 Apinaheebo · Feb 12, 2013 at 02:33 PM 0
Share

Thanks, that indeed fixed the problem. I didn't realize GameObject.Find cannot find disabled objects.

avatar image
0

Answer by asasababoon · Feb 12, 2013 at 03:34 PM

when deactivated it cant acces it. you could solve it by having an boolean in the player's script. and checking that value (if its alive or not)

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

11 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

Related Questions

How do I stop an immediate collision with all objects from ocuring at the entry of game mode? 0 Answers

how can i get a pause screen to work 0 Answers

If you disable a gameobject, does an InvokeRepeating loop end or pause? 3 Answers

[SOLVED] How I can detect deleted gameobject 1 Answer

How to determine the direction of an object to the player. 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