• 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 thiagoeo · Jul 01, 2014 at 06:37 PM · prefab-instance

Instantiate function returning NullReferenceException

I know there are many similar questions here, but none of the answers I found apply to my problem, it seems like it's all perfect, but still i get the error.

 public var target_prefab : GameObject;
 
     function Update (){
         if(hit){
             Instantiate (target_prefab,  Vector3(Random.Range(-200,200),Random.Range(46,250),Random.Range(-600,100)), Quaternion.Identity);
             points++;
             hit = false;
         }
     }

The target_prefab is properly set on the inspector. The hit boolean is set true by another script. I've debugged it, and it is fine. I tried putting the bugged line (the instantiate function) in another function out of update, tryed to atribute it to a GameObject variable (var example : GameObject; example = Instantiate...), tryed putting that line on another script, changing the random values with simple ints, but i can't seem to figure out the problem.

I'm sorry if this is a redundant, or stupid question and i didn't realize. Thanks in advance for any help.

Comment
Add comment · Show 5
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 Kiwasi · Jul 02, 2014 at 04:20 AM 1
Share

Try a debug.log of target_prefab immediately before the instantiate.

avatar image pumpkinszwan · Jul 02, 2014 at 04:44 AM 0
Share

Try recreating the gameobject from scratch in the inspector and delete the original. I have had issues before where for some reason the gameobject just would not recognize that I had dragged a prefab into my script's public field, even though it looked correct.

$$anonymous$$aybe even recreate the entire scene.

avatar image Firedan1176 · Jul 02, 2014 at 05:41 AM 0
Share

Try placing your prefab in a folder called 'Resources' under your Assets folder. Then in your script, replace target_prefab with the name of your prefab surrounded by quotes ("Target Prefab" for example)

avatar image Aarukka · Jul 02, 2014 at 05:48 AM 0
Share

You could also try setting the prefab as something else than gameobject - like transform or another specific class that it represents.

avatar image thiagoeo · Jul 02, 2014 at 10:56 PM 0
Share

Bored$$anonymous$$ormon Debug.Log(target_prefab.name); immediately before the instantiate returned the prefab's correct name (argola).

pumpkinszwan I replaced the prefab in the assets with an updated version of it that I had on the scene, and it didn't work, but I wanna try some more things before recreating the whole scene, beacuse that would take a lot time.

Firedan1176 I placed the prefab in a Resources folder, but I'm not sure I did the code part right. First I commented the declaration of the prefab and changed the Instantiate line to: Instantiate ("argola", Vector3... and it didn't work, then I uncommented the declaration and changed the reference name to "argola", but it also didn't work.

Aarukka I set it as a Transform, and it still didn't work.

Thank you all for the answers. Is there any other thing I could try before making the whole scene again or trying to code this part without the Instantiate function?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lazdude17 · Jul 02, 2014 at 05:57 AM

What scripts do you have on the prefab you are instantiating? Did you apply any changes on your prefab object in your scene that perhaps have not been made to the base prefab? Could you post the error itself with the code in question?

Comment
Add comment · Show 2 · 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 thiagoeo · Jul 02, 2014 at 11:01 PM 0
Share

The error is:

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

the line is the one with the Instantiate, and i believe the only code that has to do with it is the one I posted in the question, but if you think it's nescessary to find the problem, I can post the whole code.

I have a script on the prefab with the following code: private var player : GameObject;

 function Awake(){
     player = GameObject.FindWithTag("Player");
     GetComponent("SmoothLookAt").target = player.transform;
 }
 
 function OnTriggerEnter(other : Collider){
     player.GetComponent("GameController").hit = true;
     Destroy(gameObject);
 }

The only changes I did to the prefab on scene where in this script, but I already tryed updating the base prefab and it didn't work. Thank you for the answer

avatar image Lazdude17 · Jul 04, 2014 at 06:17 PM 0
Share

Quaternion.Identity should be Quaternion.identity

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Experiencing NullReferenceException only in build with Coroutine 1 Answer

How to create Cirlce/Box but not Collider? 1 Answer

Timer not working in prefabs 0 Answers

Prefab Duplication issue 1 Answer

How to instantiate a prefab declared in one script, from another script 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