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 /
This question was closed Apr 29, 2016 at 06:36 PM by Paul2357 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Paul2357 · Apr 13, 2016 at 02:18 PM · unity 5platformerprojectile2.5dprojectiles

2.5D Projectile Help

I'm working on a 2.5D game for senior project at my school. I have the script working to where it instantiates the object I am using. However, I have two issues (that may very well be related).

1) I keep getting this error

NullReferenceException: Object reference not set to an instance of an object PlayerAttack.Update () (at Assets/Scripts/PlayerAttack.cs:90)

I understand that it's telling me something isn't right at line 90, but I cannot figure out why it's throwing the error.

2) The force I'm adding the the projectile doesn't seem to be working. I even disabled gravity on the rigidbody to see if that made any effect (it didn't).

Any pointers in the right direction would be greatly appreciated!

Here is the code I am working on

 #region Notes Section
 /*
 
 Author: Paul Christopher
 Use: This code will:
         -Handle the different attacks(or spells) a player will have at their disposal
         -Check to see if the player is switching (or switched) to a different spell
         -Keep track of the zone the player is in and allow only a certain number of spells to be used (Functionality will be added later on)
         -Instantiate the prefab or particles to show the attack to the player
 
 Notes: None at this time
 
 */
 #endregion
 
 
 using UnityEngine;
 using System.Collections;
 
 public class PlayerAttack : MonoBehaviour {
 
     #region Variables
     //Variables for different HUD based things 
     int health, magicLevel, slavesFreed, zoneNumber;
     public int attackNumber = 1;
 
     //Projectile speed
     float bulletSpeed = 100;
     //public Rigidbody projectile;
 
     //Variables to hold the different attacks
     public GameObject fireProjectile;
     //GameObject projectile;
 
     //Projectile Test Variables
     public GameObject projectileSpawn;
     #endregion
 
     #region Start
     // Use this for initialization
     void Start () {
     }
     #endregion
 
     #region Update
     // Update is called once per frame
     void Update ()
     {
 
         //Checks for keyboard input to cycle attack number down
         if (Input.GetKeyDown(KeyCode.Q))
         {
             //Cycles number as long as it is greater than zero
             if (attackNumber > 1)
             {
                 attackNumber -= 1;
             }
         }
 
         //Checks for keyboard input to cycle attack number up
         if (Input.GetKeyDown(KeyCode.E))
         {
             //Cycles number as long as it is less than 6
             if (attackNumber < 6)
             {
                 attackNumber += 1;
             }
         }
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             //Test 1
             //Instantiate(fireProjectile, transform.position, Quaternion.identity);
             //Rigidbody rb = fireProjectile.GetComponent<Rigidbody>();
             ////rb.AddForce(500, 200, 0);
             //rb.velocity = (transform.right * -1) * bulletSpeed * Time.deltaTime;
 
             //Test 2
             //Rigidbody instantiateProjectile = Instantiate(fireProjectile, transform.position, transform.rotation) as Rigidbody;
             ////instantiateProjectile.velocity = transform.TransformDirection(new Vector3(500, 5, 0));
             //instantiateProjectile.AddForce(500,10,0);
 
             //Test 3
             //Instantiate(fireProjectile, transform.position, Quaternion.identity);
             //fireProjectile.transform.position += new Vector3(10, 0, 0);
 
             //Test 4
             //Rigidbody hitPlayer;
             //hitPlayer = Instantiate(fireProjectile, transform.position, transform.rotation) as Rigidbody;
             //hitPlayer.AddForce(new Vector3(-1000, 0, 0));
             ////hitPlayer.velocity = transform.TransformDirection(Vector3.forward * 500);
             //Destroy(this.gameObject);
 
             //Test 5
             //Transform clone;
             //clone = Instantiate(fireProjectile, transform.position, transform.rotation) as Transform;
 
 
 
         }
 
         //Handle 
         switch (attackNumber)
         {
             case 1:
                 break;
             case 2:
                 break;
             case 3:
                 break;
             case 4:
                 break;
             case 5:
                 break;
             case 6:
                 break;
         }
 
     }
     #endregion
 
     // END OF FILE BELOW
 }
 

The specific area I'm having an issue with is here:

  if (Input.GetKeyDown(KeyCode.Space))
             {
                 //Test 1
                 //Instantiate(fireProjectile, transform.position, Quaternion.identity);
                 //Rigidbody rb = fireProjectile.GetComponent<Rigidbody>();
                 ////rb.AddForce(500, 200, 0);
                 //rb.velocity = (transform.right * -1) * bulletSpeed * Time.deltaTime;
     
                 //Test 2
                 //Rigidbody instantiateProjectile = Instantiate(fireProjectile, transform.position, transform.rotation) as Rigidbody;
                 ////instantiateProjectile.velocity = transform.TransformDirection(new Vector3(500, 5, 0));
                 //instantiateProjectile.AddForce(500,10,0);
     
                 //Test 3
                 //Instantiate(fireProjectile, transform.position, Quaternion.identity);
                 //fireProjectile.transform.position += new Vector3(10, 0, 0);
     
                 //Test 4
                 //Rigidbody hitPlayer;
                 //hitPlayer = Instantiate(fireProjectile, transform.position, transform.rotation) as Rigidbody;
                 //hitPlayer.AddForce(new Vector3(-1000, 0, 0));
                 ////hitPlayer.velocity = transform.TransformDirection(Vector3.forward * 500);
                 //Destroy(this.gameObject);
     
                 //Test 5
                 //Transform clone;
                 //clone = Instantiate(fireProjectile, transform.position, transform.rotation) as Transform;
     
     
     
             }


Comment
Add comment · Show 4
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 Ali-hatem · Apr 14, 2016 at 12:06 PM 0
Share

so in which Test the error . but as i see you are trying to add force or velocity to fireProjectile from this code much better way that you attach a script to fireProjectile which contain movment code ins$$anonymous$$d so it will move once it been instantiated . try Test 1 with one line of code :

Instantiate(fireProjectile, transform.position, Quaternion.identity);

& in fireProjectile script :

 Rigidbody rb;
 float bulletSpeed = 100;
 void Start(){
 rb = GetComponent<Rigidbody>();
 }
 void Update(){
  rb.velocity = new Vector3(bulletSpeed * Time.deltaTime, 0, 0);
 }
avatar image Paul2357 Ali-hatem · Apr 14, 2016 at 01:19 PM 0
Share

I'll give that a shot and see if it works! Thank you so much for the input

avatar image Paul2357 Paul2357 · Apr 14, 2016 at 02:52 PM 0
Share

Also, the error seems to occur in all of the tests. For some reason as soon as the code is executed, the value becomes null for the projectile prefab I am trying to instantiate. Then when I set it, it still throws the error, so I'm not sure what is going on there.

avatar image Paul2357 Ali-hatem · Apr 14, 2016 at 06:50 PM 0
Share

@ali

This seems to work, the only issue I'm getting is that I now need to figure out how to get it to shoot in front of my model depending if it is moving left or right. Thanks for the input!

1 Reply

  • Sort: 
avatar image
0

Answer by Immanuel-Scholz · Apr 14, 2016 at 02:26 PM

fireProjectile is of type "GameObject" but you are trying to get it "as Rigidbody"

The "as" operator in line 89 means basically: "Dear compiler. Please check whether the object 'before the as' might be actually of type 'after the as' and if so, just assign it as this type. If not, assign null".

Since GameObject is not a Rigidbody, your dear compiler decided to assign null. (GameObject is a totally different class than Rigidbody. Rigidbody is a component attached to a GameObject. But so is Transform. Or any other script you add.)

What you probably wanted is a call to GetComponent instead.

 hitPlayer = Instantiate(fireProjectile, transform.position, transform.rotation).GetComponent<Rigidbody>();

Alternatively, you could change the type of "fireProjectile" to be "Rigidbody" and then Instantiate would return the correct component type already.

Your second problem (force is not working) is most definetely related to this one, as it crashes at the line 90 so it never sets the force in the first place.

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 Paul2357 · Apr 14, 2016 at 02:58 PM 0
Share

@Immanuel

Thanks for the input. That definitely makes sense. I don't have a ton of dedicated program$$anonymous$$g experience with instantiating through Unity, so this is a bit of a learning experience too. I will make any edits as soon as I am home from class and see if that fixes the issues I am having!

Follow this Question

Answers Answers and Comments

81 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

Related Questions

Problem with projectile direction 0 Answers

2.5D platformer character turn around (180) 0 Answers

"buddies" multiplying, can't find what's wrong with my script 1 Answer

why when the player position changed, the player get stuck in that positon,Why when i change the Player position to an object, the player get stuck 0 Answers

Where Can I Find Complete 2.5d platformer course? 0 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