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 Nighty22Night · Feb 16, 2014 at 08:16 PM · nullreferenceexceptionobject-reference-not-set

NullReferenceException: Object reference not set to an instance of an object

Hello! Firstly I thank you in advance because I am really stuck. I do not usually ask questions because usually I can find the solution by searching the internet.

Here is the error I get and this is my script. NullReferenceException: Object reference not set to an instance of an object Gun.Update () (at Assets/Gun.cs:43)

 using UnityEngine;
 using System.Collections;
 
 public class Gun : MonoBehaviour {
     Vector3 position = Vector3.zero;
     public Rigidbody2D projectile;
     private Vector3 positionmouse = Vector3.zero;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
         if ((Input.touchCount > 0 && 
              Input.GetTouch(0).phase == TouchPhase.Began)) 
         {
             position.x = transform.position.x;
             position.y = transform.position.y;
             position.z = 0;
             Vector3 positiontouch = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0);
             Vector3 look = Camera.main.ScreenToWorldPoint(positiontouch);
             Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;
             go.transform.LookAt(look);    
             
             go.AddForce(go.transform.forward * 1100);
             
         }
         
         if((Input.GetButtonDown("Fire1")))
         {
             
             position.x = transform.position.x;
             position.y = transform.position.y;
             position.z = 0;
             positionmouse.x = Input.mousePosition.x;
             positionmouse.y = Input.mousePosition.y;
             
             Vector3 look = Camera.main.ScreenToWorldPoint(positionmouse);
             Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;
             go.transform.LookAt(look);    
             
             go.AddForce(go.transform.forward * 1100);
             
             
         }
         
     }
 }

I modified my code many times to find the solution, but to no avail. The code above is the latest version, I have not been remodified.

Thank you in advance!

Comment
Add comment · Show 1
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 getyour411 · Feb 16, 2014 at 09:29 PM 0
Share

Why slice up Input.mousePosition only to rebuild it as a parameter pass for Camera.main..? Do

 Vector3 look = Camera.main.ScreenToWorldPoint(Input.mousePosition);

I haven't used Instantiate with a cast to Rigidbody2D but it seems ok; did 'go.transform' autocomplete in $$anonymous$$ono? $$anonymous$$aybe try go.gameObject.transform

3 Replies

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

Answer by FuzzyLogic · Feb 17, 2014 at 06:37 AM

You are mixing up the RigidBody2D and the GameObject references.

Replace line 43 to 45 with the following lines:

 if (go == null) return; // oops, no object instantiated! bail out
 
 go.gameObject.transform.LookAt(look);
 
 go.AddForce(go.gameObject.transform.forward * 1100);
Comment
Add comment · Show 7 · 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 Nighty22Night · Feb 17, 2014 at 06:48 AM 0
Share

Hi FuzzyLogic! I tried to enter your code. In fact there is no more crash, the projectile is instantiated but the "lookat" and "addforce" do not work.

avatar image FuzzyLogic · Feb 17, 2014 at 07:13 AM 0
Share

Those lines will never be executed if 'go' is null. Test that the code is getting past the null check by adding this line just before the LookAt line:

 Debug.Log("'go' is not NULL");

You should see that message flooding your Console window when you shoot. If you don't, then there is something wrong with your Instantiate.

avatar image Nighty22Night · Feb 17, 2014 at 07:23 AM 0
Share

I fixed part of the problem, but I am confronted with another. I think the prefab was corrupted because I have deleted and recreated and this time the instance of the projectile was not null. However, the other problem is that the instance that is created moves very slowly and does not have the right rotation I took.

alt text alt text alt text

How can i fix this please ? :/

avatar image FuzzyLogic · Feb 17, 2014 at 07:45 AM 0
Share

If go is null, then you should check that projectile is not null before calling Instantiate.

 if (projectile == null) return; // oops! bail out
 Debug.Log("'projectile' is not NULL");
 Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;
avatar image FuzzyLogic · Feb 17, 2014 at 08:01 AM 0
Share

Your instanced projectile is Looking at the mouse cursor. Did you mean to align it to always face the camera?

Show more comments
avatar image
0

Answer by mattyman174 · Feb 16, 2014 at 10:55 PM

 public Rigidbody2D projectile;
 
 Rigidbody2D go = Instantiate (projectile, position, Quaternion.Euler(0,0,0)) as Rigidbody2D;

Do you have a Rigidbody assigned to this Public Variable before playing the Script?

Otherwise your trying to Instantiate a Null Object.

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
avatar image
0

Answer by Nighty22Night · Feb 17, 2014 at 06:24 AM

Thank you for your very quick answers! From your advice, I changed the code like this:

 if(look != null)
             {
                 go.gameObject.transform.LookAt(look);    
                 
                 go.rigidbody2D.AddForce(go.transform.forward * 1100);
             }

I still have the same error... Projectile is indeed assigned to a rigidbody, and besides I shoot in the game, you can see the projectile appear before the crash the game.

alt text

The bomber is the "projectile".

So, How can I fix this? Again thank you for your answers

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 raptorkwok · Feb 17, 2014 at 06:42 AM 0
Share

This is not an answer. You can actually EDIT your question.

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

21 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

Related Questions

Upon clicking: Object reference not set to an instance of an object 0 Answers

Bug in Object reference ? 0 Answers

nullreference exeptio, object reference not set to an instance of an object 1 Answer

An object reference that seems pretty set-to-an-instance-of-an-object to me 0 Answers

NullReference on textfield 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