• 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 jonasty19 · Apr 06, 2012 at 01:39 PM · prefabinstantiation

Script not recognizing a prefab.

Hey Everyone, I'm trying to create a script where it instantiates a prefab. I'm getting an error "The name does not exist in its current context". Now, I literally have the same exact code as one of my other scripts, but I'm fairly new to Unity, so I am probably doing something obviously wrong somewhere. My code is below. I'm getting an error on Prefab1. I've tried dragging Prefab1 onto dd in the inspector, set it to None, and I still get that same error. Any help would be appreciated!

Thanks!

 using UnityEngine;

using System.Collections;

public class FireButton : MonoBehaviour {

 public GameObject dd;
 // Use this for initialization
 void Start () {
     dd = (GameObject)Instantiate(Prefab1);
 }
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

2 Replies

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

Answer by Kleptomaniac · Apr 06, 2012 at 02:08 PM

I'm not savvy with C#, but first of all check that your filename is the same as your class name (i.e. filename should be Firebutton). Second, I would suggest that you set your prefab object in the inspector like this:

 public Transform prefab1;

And then make your Instantiate line (also including position and rotation parameters):

 GameObject dd = (GameObject)Instantiate(prefab1, transform.position, Quaternion.identity);

That may not fix your problem though. It may add to your problems :D

Hope that helps, Klep

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 jonasty19 · Apr 06, 2012 at 09:34 PM 0
Share

Awesome! I knew it was something obvious that I was omitting! Thanks a lot!

avatar image Kleptomaniac · Apr 07, 2012 at 07:09 AM 0
Share

No worries! Glad I could help. :)

avatar image
0

Answer by Kryptos · Apr 06, 2012 at 02:20 PM

@kleptomaniac is right

You are trying to instantiate a prefab through a reference variable named prefab1, but the variable itself is never declared.

So, like @kleptomaniac wrote, add this line:

 public GameObject prefab1;

Then in Unity Editor, drag drop your prefab to this reference so that the variable now has a correct value.

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 Kleptomaniac · Apr 06, 2012 at 02:27 PM 0
Share

Haha, yay! I just learnt the C# way to do it then, so I'm happy. :D

avatar image jonasty19 · Apr 06, 2012 at 09:34 PM 0
Share

Yeah, thanks for the C# method!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to find a specific clone of a prefab and access its children 0 Answers

VUFORIA: Playing an animation for a instanced model? 2 Answers

GameObject variable points to instance instead of prefab when prefab gets instantiated 2 Answers

Why does a prefab instance not collide, when prefab collisions work fine? 0 Answers

ArgumentNullException when instantiating prefabs using generic list 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