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 ProNerdPanda · Mar 25, 2013 at 11:14 PM · prefabbulletcubespawnpointthunder

Nullreference?

I am trying the following:

Whenever the spacebar is pressed, a Thunderball spawns from the opponents head (through a sphere spawnpoint) and add a force to move down, like a thunder strike

i have been coding for a couple of minutes and already have a problem

 // Cast Thunder on the Cube
     if(Input.GetButtonDown("Jump"))
     {
         var ThunderBullet = Instantiate(ThunderBullet, GameObject.Find("SpawnPoint").transform.position, //something to cast the thunder to go down// );
     }

Here is the code; I get the Nullreference error (object reference not set to an instance of an object)

Here is what i'm using to achieve this:

Cube (Opponent, Standing still)

Sphere (SpawnPoint, child of ^Cube^)

Capsule (Character)

ThunderPrefab (The Thunderball)

var Thunderbullet:Transform; (on the script)

Can anyone help me here? I am equal to a sloth when it comes to coding

Comment
Add comment
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

1 Reply

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

Answer by CHPedersen · Mar 25, 2013 at 11:17 PM

Either the prefab for ThunderBullet has not been set (typically done through the editor) or a GameObject by the name "SpawnPoint" does not exist in the scene at the moment this code is run.

My guess is the latter, because I think nullreferences from unset prefabs trigger a special message about the prefab being null. So it's probably the SpawnPoint. Put it in a call to Debug.Log before trying to use it and see what it returns.

Comment
Add comment · Show 11 · 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 ProNerdPanda · Mar 25, 2013 at 11:36 PM 0
Share

alt text

alt text

I'm trying like crazy, i can't find what's wrong with the code

Spawn is called spawn, and the prefab is set (Yes, i redid the code by scratch, hoping i got something wrong before)

thunder.png (13.6 kB)
inspector.png (46.1 kB)
avatar image CHPedersen · Mar 25, 2013 at 11:46 PM 0
Share

Be literal when referring to GameObjects and their names. When you say "Spawn" is called "spawn", is that what you literally mean? Because then that's your error. GameObject.Find is case sensitive. If the one you want is called "spawn", then GameObject.Find("Spawn"), with a capital S, will return null.

avatar image ProNerdPanda · Mar 25, 2013 at 11:58 PM 0
Share

no that was a mistake by me

Spawn is Literally called Spawn

alt text

alt text

hierarchy.png (6.0 kB)
project.png (42.6 kB)
avatar image CHPedersen · Mar 26, 2013 at 12:11 AM 0
Share

Okay. GameObject.Find should find that. I can see why that might be confusing. Thankfully, the code you've posted is so wonderfully simple that it must be either the call to GameObject.Find or the prefab which is null. There is no other place in that script where an object is used in a manner that can throw a NullReferenceException. Thus, as suggested before, you need to put calls to Debug.Log before the call to Instantiate to ascertain which one it is. Do this:

     // Cast Thunder on the Cube
     if(Input.GetButtonDown("Jump"))
     {
         Debug.Log("State of Thunder: " + Thunder);
         Debug.Log("State of Spawn: " + GameObject.Find("Spawn"));
         var Thunder = Instantiate(Thunder, GameObject.Find("Spawn").transform.position, Quaternion.Identity);
     }

And pay attention to what the log says. One of them is null. If the log stays empty, then this code is never even called and your exception is thrown elsewhere.

avatar image ProNerdPanda · Mar 26, 2013 at 12:19 AM 0
Share

I'll post the log now

alt text

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

12 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

Related Questions

Need Help with Shooting Script c# 5 Answers

2D Sending Object From One Place To Mouse Position. 1 Answer

My object acts wierd, 2 balls being shot instead of one (from different position) 2 Answers

Gun Script 1 Answer

Pre-Normalized cube model? 3 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