Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ashton-Benedict · Jul 21, 2016 at 08:57 PM · c#2d-platformerbulletgunreferencing

2D Bullet Not Working

I have a 2D platformer and am using C#. And I'm trying to shoot my bullet prefab out of my gun. So basically, when I shoot my gun the bullet spawns but doesn't move, it gets stuck in mid-air. And second the bullet sprite wont flip to go in the direction I shoot it, it only spawns in one direction. So my entire script that goes on my bullet prefab doesn't work. And one of the reasons is because I have a reference problem, I don't have a bullet prefab in my inspector (when I did, the bullet would fly off, it kind of worked. But also the bullet would move in the direction I would move, if I shot right and then walked left, the bullet would change directions and travel left). So my reference to the script for the bullet I have on a script attached to my player says that its null because it cant find the prefab or whatever. I don't know what to do, I've given up, I've tried everything... Please help.

SCRIPT ATTACHED TO PLAYER:

 public class Glock18 : MonoBehaviour {
 
     public float fireSpeed = 0f;
     public float fireRate = 0f;
     public float damage = 10f;
     public LayerMask whatToHit;
 
     public Transform firePoint;
     public Transform bulletTrail;
 
     private WeaponPickUp weaponPickUp;
     private BulletTrail bulletScript;
 
     void Awake()
     {
         weaponPickUp = transform.root.GetComponent<WeaponPickUp>();
         bulletScript = transform.GetComponent<BulletTrail>();
     }
 
     void Update()
     {
         if (weaponPickUp.glockE == true)
         {
                 if (fireRate == 1 &&  Input.GetButtonDown("Fire1") || Input.GetKeyDown(KeyCode.L))
                 {
                 Debug.Log("is bulletScript null? " + (bulletScript == null));
                 Shoot();
                 }
         }
     }
 
     void Shoot()
     {
         Vector2 firePointPosition = new Vector2(firePoint.position.x, firePoint.position.y);
         RaycastHit2D hit = Physics2D.Raycast(firePointPosition, firePointPosition, 10, whatToHit);
         Effect();
     }
 
     void Effect()
     {
         if (bulletScript)
         {
             Debug.Log("Move");
             bulletScript.GetComponent<BulletTrail>().Move();
         }
         Instantiate(bulletTrail, firePoint.position, firePoint.rotation);
     }
 }

SCRIPT ATTACHED TO BULLET:

 public class BulletTrail : MonoBehaviour {
 
     public int moveSpeed = 0;
     public bool startingRight = true;
 
     private PlayerController playerController;
 
     void Awake()
     {
         playerController = GameObject.FindObjectOfType<PlayerController>();
     }
 
     void Start()
     {
         if (playerController.facingRight)
         {
             startingRight = true;
         }
         else if (!playerController.facingRight)
         {
             startingRight = false;
         }
     }
 
     public void Move()
     {
         
             if (!playerController.facingRight)
             {
             MoveLeft();
             }
             else if (playerController.facingRight)
             {
             MoveRight();
             }
     }
 
     void MoveLeft()
     {
         if (startingRight)
         {
             Debug.Log("FlipL");
             Flip();
         }
             gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(Time.deltaTime * moveSpeed * -50, GetComponent<Rigidbody2D>().velocity.y);
             Destroy(gameObject, 1);
     }
 
     void MoveRight()
     {
         if (!startingRight)
         {
             Debug.Log("FlipR");
             Flip();
         }
             gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(Time.deltaTime * moveSpeed * 50, GetComponent<Rigidbody2D>().velocity.y);
             Destroy(gameObject, 1);
     }
 
     void Flip()
     {
         startingRight = !startingRight;
         Vector3 scale = transform.localScale;
         scale.x *= -1;
         transform.localScale = scale;
     }
 }
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 rmassanet · Jul 22, 2016 at 07:05 AM 0
Share

Could you show us the script where the null reference error is thrown?

avatar image Ashton-Benedict rmassanet · Jul 22, 2016 at 04:45 PM 0
Share

There is no null reference error its that the way my reference is set up causes my bullet script not to work, Its confusing. :/

Because when I had the prefab in my inspector, the script would work for that one prefab. But not the ones spawned from my "FirePoint".

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TheShadyColombian · Jul 22, 2016 at 06:14 PM

Whenever you get a Null reference error (or any error, in fact) the script will stop and any code after the error will not run. Check the console and fix EVERY error (or add try/catch statements). The console will tell you what line the error is in (/path/to/script: errorLine) or just double click it, and it will place the cursor on the line with the error. try commenting that line.

Let me know what happens.

Comment
Add comment · Show 9 · 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 Ashton-Benedict · Jul 22, 2016 at 08:19 PM 0
Share

I said in my response to the other guy, I don't have a null error, I don't have any errors. It just doesn't work...

avatar image TheShadyColombian Ashton-Benedict · Jul 23, 2016 at 10:17 PM 0
Share

I think I see the problem. So just to clarify, the only problem with specifying the prefab in the inspector is that it sets the direction of the bullet even after being shot out of the gun?

avatar image Ashton-Benedict TheShadyColombian · Jul 25, 2016 at 03:34 AM 0
Share

when I shoot the gun, the bullet always fires right, the bullet sprite always faces right, and the bullet changes direction if I change direction.

Show more comments

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

204 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 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

Unity 2d simple c# shooting script 1 Answer

2D Platformer Gun Equip Script Not Working 0 Answers

How To Do Basic 2D Movement? 1 Answer

Random Number keeps generating over and over 2 Answers

Advance Colision Detection 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