Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Expendis · Mar 16, 2010 at 06:40 PM · instantiateraycastprefabnull

The prefab you want to instantiate is null.

In my scene, I have a prefab of a particle (also tested it with a plain sphere to no avail) that I am trying to instantiate at the collision of a ray (as a bullet explosion).

I figured I would get this error if the prefab didnt contain any objects but that is not the case. (The prefab icon is blue)

This is just another flaw in my code, I bet, that results from my lack of experience. Thanks in advance for helping!

Ive attached this script to the object that is shooting the ray that results in the collision.

var gun : Transform; var layerMask = 1 << 8; var loc : Vector3; var bulletspark : Transform;

function Update () {

//position of the gun in the world var gunPosition : Vector3 = gun.position; // distance calculation assumes camera has 0, 0, 0 rotation var distanceFromCamera : float = gunPosition.z - camera.main.transform.position.z; var screenPos : Vector3 = Input.mousePosition; screenPos.z = distanceFromCamera; // calculate where in the game-plane the bullet must go to var worldPos : Vector3 = Camera.main.ScreenToWorldPoint(screenPos); // calculate bullet direction... var dir : Vector3 = (worldPos - gunPosition).normalized;

if (Input.GetButtonDown ("Fire1")) { var hit: RaycastHit; if (Physics.Raycast (gunPosition, dir, hit)) { //cast the ray var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal); //determine the rotation of the collision loc=hit.point; //determined the location of the collision loc.z = 0; Debug.DrawRay (gunPosition, dir);//debug print(rot);//debug print(loc);//debug Instantiate(bulletspark, loc, rot);//create sparks } } }

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by duck · Mar 16, 2010 at 08:49 PM

From everything you've described so far, it would appear that the particular instance you're examining is set up entirely correctly.

Which leads me to think, could it be that you've accidentally dropped that script onto some other GameObject in your hierarchy too, without realising? If so, it may be that instance which is throwing the error (as opposed to the one you're inspecting, which seems fine).

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
1

Answer by Expendis · Mar 16, 2010 at 09:18 PM

Thanks so much for your answers!

My problem is solved.

The problem was actually that I was dragging the Bulletspark prefab into the script and not the instance of the script in the object that fires the raycast.

I predicted that this was a problem related to a misunderstanding in the interface.

Once again thanks for your answers. This community is awesome.

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 Cyclops · Mar 17, 2010 at 04:18 PM 0
Share

@Expendis, it's nice to say thanks, but for future reference, it should go into a comment in another Answer, not as a separate Answer. Also consider up-voting and check-boxing the best answer (which in this case, appears to be Duck's - he needs the points, anyway. :)

avatar image
0

Answer by Cyclops · Mar 16, 2010 at 07:44 PM

This is checking the obvious, but, you're sure that bulletspark is correctly pointing to the Prefab, in the Editor window? You did a drag/drop, or used the dropdown menu, and it's connected to the public script variable? I've had times when I accidently disconnected a variable without noticing.

Comment
Add comment · Show 3 · 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 Expendis · Mar 16, 2010 at 07:54 PM 0
Share

Yes, I have made sure of this..

http://i43.tinypic.com/2mmg407.jpg>

avatar image Cyclops · Mar 16, 2010 at 08:20 PM 0
Share

Yes, the picture shows bulletspark as connected - but gun is null. I don't see it in the script above, is it also instantiated, and could the error message be from that code ins$$anonymous$$d?

avatar image duck ♦♦ · Mar 16, 2010 at 08:51 PM 0
Share

Hm, yes you're not trying to instantiate the gun, but you are using it to fill the variable "gunPosition". If 'gun' isn't assigned, you should get a null reference error though, not a Prefab is null error. Strange.

avatar image
0

Answer by duck · Mar 16, 2010 at 08:05 PM

Have you changed the type of the variable 'bulletspark' since you made the drag-reference? For example, perhaps it used to be:

var bulletspark : GameObject;

...or something else, when you made the drag-reference, and then maybe you changed the type to:

var bulletspark : Transform;

If so, you have to re-drag the reference even though it appears to still be linked. I can't remember off the top of my head whether this generates the error that you describe, or a different error, but it's one more thing to check.

If you can't remember whether you changed it, just try re-dragging the reference anyway and see if it fixes it. If it does, that was most likely the cause of the problem.

Comment
Add comment · Show 4 · 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 Expendis · Mar 16, 2010 at 08:14 PM 0
Share

I havent changed the variable type but to be sure, I dragged it in again and Im still getting the same error.

Thanks for your help so far!

avatar image Cyclops · Mar 16, 2010 at 08:22 PM 0
Share

@Duck, that's a good point - why is it a Transform, anyway? A Prefab is really a GameObject. If you didn't change it, @Expendis, might try it.

avatar image Expendis · Mar 16, 2010 at 08:43 PM 0
Share

I changed it to a GameObject, and to make sure that the glitch you mentioned wasnt occurring, @Duck, I dragged the prefab back onto Bulletsparks on the script. ...To no avail.

Same error, same place in the script.

I greatly appreciate your help in this! Thanks everyone.

avatar image duck ♦♦ · Mar 16, 2010 at 08:44 PM 0
Share

It should work as a transform, if you didn't change it after the drag. You can instantiate a prefab from a reference to any type of component attached to it. Whichever you use, the entire prefab will be instantiated in its complete form.

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

No one has followed this question yet.

Related Questions

Instantiate prefab from original position to click mouse point position 0 Answers

Help - Instantiate A Prefab On Mouse Position 1 Answer

ArgumentException: The prefab you want to instantiate is null. 1 Answer

Some Prefabs are not Loading 1 Answer

Initializing Prefab returns NullPointerException 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