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 bloodruns4ever · May 10, 2014 at 12:15 AM · enemyshootingbulletenemyai

Enemy shooting player driving me crazy!

Please help me, I cannot for the life of me figure out what my problem is!

So I have a game very similar to geometry wars. Enemies spawn and you as a player move around and shoot. The enemies direction and speed are completely random (to a degree). I am now trying to make a new enemy variant that shoot the player with bullets of their own. I have the Enemy_Shoot Script attached to my enemy prefab:

 using UnityEngine;
 using System.Collections;
 
 public class Enemy_Shoot : MonoBehaviour {
 
     public float bulletSpeed;
     public float fireRate;
     public float nextFire = 1;
 
     public GameObject bullet;
 
     Transform enemyBullet;
     Transform playerPosition;
 
     void Awake()
     {
         if (!playerPosition)
         {
             playerPosition = GameObject.FindWithTag("Player").transform;
         }
     }
     
     // Update is called once per frame
     void Update () {
         if (playerPosition)
         {
             if (Time.time >= nextFire)
             {   
                 nextFire = Time.time + fireRate;
                 CreateBullet();
             }
         }
     }
 
     void CreateBullet()
     {
         // Creates a new instance of the bullet
         Instantiate(bullet, transform.position + (playerPosition.position - transform.position).normalized, transform.rotation);       
     }
 }

and the EnemyBulletMovement attached to my bullet prefab:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyBulletMovement : MonoBehaviour
 {
 
     public Vector3 velocity;
     public float step = 2f;
     private Transform player;
 
     void Start()
     {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         var player = go.transform;
         Debug.Log("Player:" + "" + player);
         Debug.Log(this.transform.position);
         Debug.Log("player's position" + player.position);
     }
 
     //Update is called once per frame
     void Update()
     {
         this.transform.position = Vector3.MoveTowards(this.transform.position, player.position, step * Time.deltaTime);
         Debug.Log("this.transform:" + "" + this.transform.position);
     }
 }


However, I keep getting a Null Reference Exception in line 23 of the EnemyBulletMovement script. I've tried so many things and looked all over the internet and I just cannot figure it out -_- The bullet gets spawned, does not move, and I get that NullReferenceException. Any help would be great, thank you!!

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 Jojo B · May 10, 2014 at 12:23 AM 0
Share

not sure if this will make any difference but try declaring it as a new vector3

avatar image bloodruns4ever · May 10, 2014 at 12:36 AM 0
Share

It was because of the var in line 14 -_- I got it though, thank you

2 Replies

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

Answer by bloodruns4ever · May 10, 2014 at 12:42 AM

You know what I got it.... In line 14 where I have var player, simply getting rid of the var worked.

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 Mr.Hal · May 10, 2014 at 02:21 AM 0
Share

That would do it, Did you by any chance look at my answer? $$anonymous$$ight be helpful. Also there is a feature in $$anonymous$$onoDevelop-Unity that will give you coding suggestions, I'm pretty sure it would pick up this dumb error, I use the feature a lot. Its called Source Analysis and its under Text Editor in the options.

avatar image
0

Answer by Mr.Hal · May 10, 2014 at 12:41 AM

This is just an idea... but it would fix your problem. Make a static variable on the player that is type Player and assign it as soon as the script runs via. Then use it on the bullet script. Let me know if this opens some doors for you. :D

 //Somewhere not in a function on the player script
 public static Player Player1;
 //Somewhere in Start or OnEnable on the player script
 Player1 = this;
 
 
 //Then on the bullet movement script
     void Update()
     {
         this.transform.position = Vector3.MoveTowards(this.transform.position, Player.Player1.transform.position, step * Time.deltaTime);
         Debug.Log("this.transform:" + "" + this.transform.position);
     }
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

22 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

Related Questions

[I REALLY NEED HELP FAST]Help with enemy Shooting 1 Answer

Staged bullet damage? 1 Answer

Only one of all my Enemies shoots, Help please. 1 Answer

Enemy AI problem 1 Answer

Enemy fire does not propel in any direction? 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