• 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 nife552 · Dec 25, 2011 at 10:13 PM · errorinstantiateinstanceshoot

very simple shoot script keeps getting error!!!

i made a very simple shoot script to try and shoot a fireball when I clicked, but for some reason I keep getting an error no matter what I do!

script:

var fireballPrefab : Rigidbody;

function Update () {

if(Input.GetButtonDown("Fire1")){

 var fireballInstance = Instantiate(fireballPrefab, 
 GameObject.Find("magicspawn").transform.identity, 
 GameObject.Find("Main Camera").transform.rotation);

}

}

Error:

NullReferenceException: Object reference not set to an instance of an object fireballshoot.Update () (at Assets/fireballshoot.js:7)

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 nife552 · Dec 26, 2011 at 09:12 PM 0
Share

Lo0Nuhti$$anonymous$$ was right, as soon as I deleted transform.identity, it quit giving me the error and started shooting.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Lo0NuhtiK · Dec 26, 2011 at 12:00 AM

I'm not sure that "transform.identity" is anything ... I could be wrong though. Try 'transform.position' instead.

var fireballPrefab : Rigidbody ; var magicSpawn : Transform ; //drag your spawner into this slot var cam : Transform ; //drag your camera here ; could also be set with Camera.main I suppose var fire : String = "Fire1" ;

function Update(){ if(Input.GetButtonDown(fire)){ ShootOrSomethin() ; } }   function ShootOrSomethin(){ var fireballClone : Rigidbody ; fireballClone = Instantiate(fireballPrefab, magicSpawn.position, cam.rotation) ; }

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 Lo0NuhtiK · Dec 26, 2011 at 03:24 AM 0
Share

NullReferenceException: Object reference not set to an instance of an object ts2.ShootOrSomethin () (at Assets/ts2.js:16) ts2.Update () (at Assets/ts2.js:10)
This is the error I just got right now after having set the code I posted above to instantiate at
(fireballPrefab, magicSpawn.identity, cam.rotation)

This Error Does NOT happen when it is instantiated at
(fireballPrefab, magicSpawn.position, cam.rotation)

Notice the line difference of magicSpawn.identity , and magicSpawn.position .
Both tests were ran just now WITH a rigidbody prefab assigned in the inspector panel.
it says error at line 16 because in my test script I had 2 empty lines at the top before I pasted the code above into it, in case anyone is wondering.
avatar image
0

Answer by Siegeon · Dec 26, 2011 at 02:51 AM

NullReferenceException: Object reference not set to an instance of an object fireballshoot.Update () (at Assets/fireballshoot.js:7)

This error message, like JerryCic mentioned has more to do with the fact that what ever reference you are using for you fireball is either empty or unassigned.

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 JerryCic · Dec 26, 2011 at 01:31 AM

Or... Are you passing the FireballPrefab into your routine? You do this by inspecting the game object that the script is attached to. In the instpactor you should see the placeholder for the fireballprefab. look in your prefab folder an drag the prefab in question into that placeholder slot.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"Object Reference not set to an instance of an object" 1 Answer

NullReferenceException Error 2 Answers

Shooting script issue? 0 Answers

Instanciate Object Problem for Shooting 2 Answers

Script keeps functioning when disabled 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges