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 SassMaster · Oct 13, 2014 at 11:52 PM · 2dparameter

Trouble with null object reference.

I've been trying to get some red paint to fire from a gun in a 2D, side-scrolling game, but I've been having trouble with the actual movement of the paint. As soon as I instantiate the paint at the guns barrel, it sits still despite my AddForce behavior, and apparently it's due to the FirePaint() method having its argument "GameObject p" (which will be used later in the game to detect which color of paint is equipped) it throws this error:

Object reference not set to an instance of an object PaintFiring.FirePaint (UnityEngine.GameObject p) (at Assets/Scripts/PaintFiring.cs:27) PaintFiring.Update () (at Assets/Scripts/PaintFiring.cs:20)

Here's my code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PaintFiring : MonoBehaviour {
 
     public GameObject redPaint;
     public Transform barrelEnd;
     public int paintSpeed = 5;
 
     void Start()
     {
         redPaint = GameObject.Find ("RedPaint");
     }
 
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Space)) 
         {
             FirePaint(redPaint);
         }
     }
     void FirePaint(GameObject p)
     {
         Rigidbody2D paintClone;
         paintClone = Instantiate(redPaint, barrelEnd.position, barrelEnd.rotation)as Rigidbody2D;
         paintClone.rigidbody2D.AddForce(new Vector2(paintSpeed, 0));
     }
 }    
 

I have no idea why this would throw the exception it did. I'm passing a parameter GameObject in the Update method, which is what Unity says I'm not passing. I'm probably missing something basic, so please excuse me in that regard. I'm still new to C#, and i'm not completely up to date on the differences between UnityScript and C#. I have used it outside of Unity, but I don't know how to use it in Unity very well yet...

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 richyrich · Oct 14, 2014 at 12:27 AM 0
Share

Have you checked for typos: is 'RedPaint' actually 'redPaint'?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Eric5h5 · Oct 14, 2014 at 01:05 AM

You defined redPaint as a GameObject but you're attempting to instantiate it as a Rigidbody2D. You can't cast a GameObject to a Rigidbody2D so it's just null.

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 JanusMirith · Oct 14, 2014 at 03:30 AM

 //Put this into your code to see if this is where you messed up
 //Spelling and caps matter, always make sure you have found a object after looking, unity will 
 // not through an error if it can not find the object it just returns null
 
 
  void Start()
     {
         redPaint = GameObject.Find ("RedPaint");
         //Look to see if we have found the gameobject
         if (redPaint == null){
             Debug.LogError("Can not find RedPaint");
         }
     }

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 SassMaster · Oct 14, 2014 at 06:41 PM 0
Share

I used both solutions. In good news, the paint is now flying like it should, but I'm still having the null prefab exception thrown at me everytime I play. I have to manually set the Red Paint in the prefab, and in reply to richyrich, I have checked, the code is set to look for "Red Paint", the GameObject of Red Paint is "Red Paint", but the prefab is "RedPaint". Although when I swapped it over to contain the prefab name, it still throws the error.

When I used the extra debug to check if "redPaint" was null, it returned null, but I have no idea why it would.

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

5 People are following this question.

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 convert to 2D 1 Answer

2D PlatformerController: Cumulative drift? 1 Answer

Replacement for LookAt in 2D 1 Answer

2d Game Jump Issue 2 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