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 PvTGreg · Aug 25, 2014 at 02:17 PM · nullreference

Any idea why this is spitting out null reference exceptions?

NullReferenceException: Object reference not set to an instance of an object LookAtTarget.Fire () (at Assets/Scripts/LookAtTarget.cs:26)

  using System;
     using UnityEngine;
     
     public class LookAtTarget : MonoBehaviour
     {
         public Rigidbody2D projectile;
         public float speed = 1000;
         public Transform target;
     
         void Start() 
         {
             InvokeRepeating("Fire", 3, 0.5f);
         }
         void Update()
         {
             if(target != null)
             {
                 transform.LookAt(target);
             }
     
             
         }
         void Fire()
         {    
             Rigidbody2D instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody2D;
             instantiatedProjectile.velocity = new Vector2(instantiatedProjectile.velocity.x,3);
         }
     
     }
 
Comment
Add comment · Show 11
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 · Aug 25, 2014 at 02:26 PM 0
Share

@PvTGreg, is projectile assigned in the inspector? If not, it will be null. Check during playmode

avatar image PvTGreg · Aug 25, 2014 at 02:29 PM 0
Share

Hi it is assigned it is firing them but not adding the velocity

avatar image Bojidar · Aug 25, 2014 at 02:33 PM 0
Share

Could you try to use "GameObject instantiatedProjectile=... as GameObject" ins$$anonymous$$d of "Rigidbody2D instantiatedProjectile =... as Rigidbody2D"?

avatar image PvTGreg · Aug 26, 2014 at 06:44 AM 0
Share

hi ill try this today thanks

avatar image Mayank Ghanshala · Aug 26, 2014 at 07:01 AM 0
Share

Do your projectile contain Rigidbody2d???

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Aug 25, 2014 at 02:37 PM

Add some debugging code, is it possible you're attaching somethign that isn't a rigidbody2d? Try an transform or gameobject instead.

Use this to see what the output is in the console:

 void Fire()
 { 
     if (projectile == null)
       Debug.Log("Projectile is null");
     else
         Debug.Log("Projectile is NOT null");
         
     if (transform == null)
         Debug.Log("Transform is null");
     else
         Debug.Log("Transform is NOT null");
         
     Rigidbody2D instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody2D;
     
     if (instantiatedProjectile == null)
         Debug.Log("instantiatedProjectile is null");
     else
         Debug.Log("instantiatedProjectile is NOT null");
     
     instantiatedProjectile.velocity = new Vector2(instantiatedProjectile.velocity.x,3);
 }
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

25 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Material doesn't have a color property '_Color' 4 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 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