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 /
  • Help Room /
avatar image
0
Question by LilGames · May 04, 2017 at 02:16 PM · instantiate prefabprefab-instancedynamic variables

How to instantiate a prefab using a string variable ?

I like compact code, and have had to resort to this abomination (below) because I just could not figure out how to reference a prefab name via a dynamically created string.

 if(winningsAmount >= 1000)
 {
     instance = Instantiate(chip1000, chipStartPosition, Quaternion.identity );
 }
 else if(winningsAmount >= 500)
 {
     instance = Instantiate(chip500, chipStartPosition, Quaternion.identity );
 }
 else if(winningsAmount >= 100)
 {
     instance = Instantiate(chip100, chipStartPosition, Quaternion.identity );
 }
 else if(winningsAmount >= 25)
 {
     instance = Instantiate(chip25, chipStartPosition, Quaternion.identity );
 }
 else if(winningsAmount >= 5)
 {
     instance = Instantiate(chip5, chipStartPosition, Quaternion.identity );
 }
 else{
     instance = Instantiate(chip1, chipStartPosition, Quaternion.identity );
 }


I would much rather to something like this, but I get errors and everything I have researched and try just won't work.

 string value = 1000;
 prefabName = "chip"+value;
 instance = Instantiate(prefabName , chipStartPosition, Quaternion.identity );

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Gutthegut · May 04, 2017 at 05:03 PM

That's beacause you are trying to instantiate the prefab by his name while you should call the GameObject.

You should have something like

 GameObject myPrefab = GetComponent<prefabName>;
 [...]
 instance = Instantiate(myPrefab, chipStartPosition, Quaternion.identity);

You can also do it with:

   public GameObject[] chip;
   int value = 1000
   [...]
   instance = Instantiate(chip[value], chipStartPosition, Quaternion.identity);

Hope this will help you. =)

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 LilGames · May 05, 2017 at 06:05 PM 0
Share

Thanks, but your first suggestion just doesn't work.

The second might be viable in theory but will require that each prefab has an ID. Otherwise I would need an array 1000 long in order to be able to store "chip1000" in index 1000. Not practical at all to do via the inspector, but maybe I can assign them into an array at runtime. Still not ideal though...

Surely there is a way to dynamically create a GameObject's name and then be able to reference it?

avatar image Gutthegut LilGames · May 05, 2017 at 06:45 PM 1
Share

Okay, my bad i didn't understand the question properly.

if i did undestand you want to create instance of an object and then reference it.

try something like

 GameObject[] instance;
 instance[value] = instantiate(prefab, position, rotation);

Anyway you can't create a dynamic var name with C# you'll ave to use list or array.

avatar image LilGames Gutthegut · May 05, 2017 at 07:22 PM 0
Share

I want to assign a string and be able to instantiate the prefab which matches that string.

Anyway you can't create a dynamic var name with C# you'll ave to use list or array.

But that seems to answer my question right there.

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

100 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 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 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 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 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

Instantiating two prefabs instead one (Bug) 0 Answers

Why do my first 50 instances of a prefab have the same properties? 1 Answer

applying and saving in UpgradeSystem 1 Answer

Stuck With this codes. 1 Answer

Positioning a prefab correctly with Instantiate 3 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