Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
5
Question by lampshade · May 26, 2010 at 11:32 PM · instantiateprefab

Instantiate PreFab with C# Script

Hi,

I've found online that the best way to instantiate a game object type is with the following statement:

GameObject go = Instantiate(Resources.Load("MyPrefab")); 

This does not work for me, does it require more using statements than:

using UnityEngine; using System;

I am getting the following error:

Assets/Scripts/Instantiate.cs(54,67): error CS0426: The nested type Load' does not exist in the typeUnityEngine.Resources'

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 Mike 3 · May 26, 2010 at 11:45 PM 0
Share

Besides the spelling mistake with Resources (which the compile error implies is not a problem with the original code), it looks fine - could you post the actual code?

avatar image lampshade · May 27, 2010 at 12:22 AM 0
Share

Right, overlooking the spelling mistake, this is the actual code (C#): GameObject go = Instantiate(Resources.Load("$$anonymous$$yPrefab"));

avatar image Mike 3 · May 27, 2010 at 12:30 AM 0
Share

uh... Instantiate.cs.... that can't be good.... try rena$$anonymous$$g that file or using Object.Instantiate for all instantiations from now on

avatar image lampshade · May 27, 2010 at 12:46 AM 0
Share

That didn't work either, nor did having my script under the Resources folder, with the prefabs.

avatar image lampshade · May 27, 2010 at 12:47 AM 0
Share

$$anonymous$$y goal, is to get a particle effect happening for the player, when I press a key on the keyboard.

5 Replies

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

Answer by Ashkan_gc · Jun 10, 2010 at 11:31 AM

you can create any public variable type that is a children of component or GameObject and instantiate it with this function. the return type of Instantiate is UnityEngine.Object and you should cast it if you want to asign it to a variable of type GameObject. so just (GameObject) before the Instantiate and it will work well. you should have the resources folder with the prefab in it thow.

GameObject go = (GameObject)Instantiate(Resources.Load("MyPrefab")); ;

or

GameObject go = Instantiate(Resources.Load("MyPrefab")) as GameObject; 

see MSDN topics for as keyword and topics about casting for more information.

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 _Petroz · Jun 10, 2010 at 12:01 PM 0
Share

I am using Unity Free edition and resources work fine for me.

avatar image Ashkan_gc · Jun 11, 2010 at 08:09 AM 0
Share

yes you are right. sorry about that.

avatar image Ill-Fish · Feb 17, 2016 at 06:28 PM 5
Share

the faster way is to make a public variable for the Prefab and drag your Prefab on it in the inspector and then instatiate it. Resources.Load is really slow, so you should avoid it, if you can.

 public GameObject myPrefab;
 
 GameObject go = (GameObject)Instantiate(myPrefab);
avatar image
14

Answer by _Petroz · Jun 10, 2010 at 10:58 AM

In the root of your Project hierarchy make a folder called 'Resources', put your prefab in that folder. My prefab is called DamageText and here is my working code:

GameObject textObject = (GameObject)Instantiate(Resources.Load("DamageText"));

Tested using C# with Unity 2.6.

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 kablammyman · Jun 07, 2011 at 07:48 PM 0
Share

this is the actual correct answer. Resource.Load looks in a resource folder to find the object you want to load. The error message tells you this as well (in a very confusing way):

Assets/Scripts/Instantiate.cs(54,67): error CS0426: The nested type Load' does not exist in the typeUnityEngine.Resources'

avatar image rawsome · Feb 17, 2014 at 09:22 PM 0
Share

Resource.Load also works if the Resource-Folder is not on top of your Project or Asset dir, it can by within any subdirectory if you want, i.e. Assets/Prefabs/Weapons/Resources/myPrefab is perfectly fine.

avatar image
4

Answer by user-1846 (google) · May 26, 2010 at 11:55 PM

This is what I would do

at the top put

public Transform whatYouWantToInstantiate;

then wherever you want to instantiate

Instantiate(whatYouWantToInstantiate);

That's the simple way...

Christian Stewart

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 lampshade · May 27, 2010 at 12:19 AM 0
Share

This way work for a transform varaible, but I need to instantiate a prefab. Sorry I left that unspecified.

avatar image Mike 3 · May 27, 2010 at 12:50 AM 0
Share

it should work the same - you can just drag the prefab onto the transform variable

avatar image
0

Answer by mocaponeuk · Nov 17, 2013 at 07:05 PM

im getting 'The variable `go' is assigned but its value is never used'

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 LukeWaffel · Aug 29, 2014 at 10:47 AM 0
Share

That's because you are creating a new GameObject called "go" and you are making that gameObject by instantiating. The reason it says thatb it's never used because you probably don't have anything like go.GetComponent or any other thing that you want to do with go, therefor it's assigned but you didn't use that assignment... If you get what I mean

avatar image
0

Answer by TheRealKuha · May 18, 2016 at 07:27 PM

I found a video explaining this pretty good if you still need help, also tells you how to spawn a rigidbody: https://www.youtube.com/watch?v=_Xrw2EEhzI4

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.

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

Associate objects to a prefab 1 Answer

instantiating vertically 2 Answers

Brain freeze: declaring a prefab inside the same prefab 1 Answer

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers

Instantiating one prefab with different materials in one frame 0 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