• 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 Michael_ · Dec 12, 2013 at 07:07 AM · instantiatepositioning

After instantiating an object I cannot refer to it?

public Transform prefab;
GameObject target;

 void Start () {  
     teksture = Instantiate(prefab, new Vector2(transform.position.x, transform.position.y), Quaternion.identity)as GameObject;  
 }
   
 void Update () {  
     teksture.transform.position = transform.position;  
 }  

"NullRefrenceExeption: Object reference not set to an instance of an object"

What am I doing wrong? The project is in 2D. The script doesn't notice it out side of Start function. EDIT: I added prefab I just drag and drop the prefab into place in unity. And also now I noticed that if I instantiate teksture in update function and try to call it in any way, it gives the same error. Also I should say that it does spawn it.

Comment
Add comment · Show 2
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 robertbu · Dec 12, 2013 at 07:10 AM 2
Share

You've only given us part of the script and don't give us info about the error. How is 'prefab' declared? How is 'prefab' initialized? How is 'teksture' declared? If you put a 'Debug.Log(teksture)' just below line 4, does it show a null value or is it being initialzied?

avatar image Theinsanekiller · Dec 12, 2013 at 12:38 PM 0
Share

Have you declared teksture anywhere in your code.?

2 Replies

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

Answer by haim96 · Dec 12, 2013 at 09:05 AM

"public Transform prefab; " should be -> "public GAMEOBJECT prefab;"

if you instantiate prefab it need to be gameobject type not transform.

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 AlucardJay · Dec 12, 2013 at 09:20 AM 1
Share

Actually, you can instantiate from many components : transform, rigidbody, scripts, etc etc.

The problem is as stated by robertbu : teksture is not declared as a variable, where it should be declared as a global variable.

avatar image haim96 · Dec 12, 2013 at 09:45 AM 0
Share

ohhh... O$$anonymous$$. i never tried it before. good to know!

avatar image Michael_ · Dec 12, 2013 at 10:56 AM 0
Share

This worked!

avatar image haim96 · Dec 12, 2013 at 11:04 AM 0
Share

Great! If you really can instantiate from transform I think it didn't worked for you because you tried to instantiate as gameobject ins$$anonymous$$d of transform. In other words you tried to convert transform type to gameobject type.

avatar image
0

Answer by lighting · Dec 12, 2013 at 08:14 AM

The problem is that Instantiate() method didn't work and returned null. Thus teksture is 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

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I position an instantiated row of prefabs? 1 Answer

Getting size of instantiated objects. 0 Answers

2D Space Shooter Instantiating bullet question 1 Answer

Fixing instantiated object in one direction? 0 Answers

How can I get an accurate gameObject count within a frame? 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