• 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 JVILL · Jan 30, 2013 at 01:42 PM · instantiateconvertnamespacename

Convert String into Variable name

var fHigh1 : Transform;

 function OnTriggerEnter ( other : Collider )
 {
 r = other.transform.position.x;
 c = other.transform.position.z;
 var name = other.gameObject.name;
 Instantiate (name, Vector3(r, 0, c), transform.rotation);
 }

This doesn't work. Gives BCE0023. If I could turn that .name string into the variable name I could save over 80 lines of code. Please help. :)

Comment
Add comment · Show 11
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 Dave-Carlile · Jan 30, 2013 at 01:45 PM 0
Share

Why wouldn't you just do Instantiate(other.gameObject, Vector3(r, 0, c), transform.rotation)? Instantiate clones an object, which is what you're trying to do it appears.

avatar image JVILL · Jan 31, 2013 at 09:27 AM 0
Share

Because 'other' in this case is an empty Game Object, and it just so happens it has the same exact name as the Terrain piece I want to spawn at its location.

avatar image marius.treu · Jan 31, 2013 at 09:51 AM 0
Share

It sounds like a little workaround. It would help if you just upload a screenshot of the resources folder + the other GameObject in hierachy view.

avatar image JVILL · Jan 31, 2013 at 11:09 AM 0
Share

alt text link text

Also note, I'm currently working on a simultaneous problem, here: http://answers.unity3d.com/questions/390996/instantiate-terrain-object-as-child-of-empty-game.html

Basically, I have a gameObject collider parented to player, 500 units below playing field. A number of empty game objects (named after their tile counterparts) are also down there, and when the players box collides with the other game objects, we generate a Terrain piece above at height 0. I want to make the Terrain piece parented to the counterpart gameObject, also.

When the player collision box leaves that game object (onTriggerExit), the child is destroyed.

Phew! :O

Basically just a Diablo 2 style game and the game only creates Terrains the camera can see. :)

tilecollider.txt (12.1 kB)
1.png (20.2 kB)
avatar image marius.treu · Jan 31, 2013 at 12:20 PM 0
Share

okay.. first i would recommend using else if ins$$anonymous$$d of using if for each (it's a kind of performance thing, if the gamename was found you dont need to go on) second that structure is horrible :D . Try to do it more dynamically (using lists e.g.). You dont have to register the gameObjects. Resources.Load(string) will return an gameobject for you that you can instanciate. Thrid there are 3 possibilitys why it wont work

  • your tileDestoryer (called on triggerExit) is messing up

  • your linked gameobjects at the top are wrong

  • your trigger collider is messing up

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by marius.treu · Jan 30, 2013 at 02:25 PM

you cannot instanciate a string... i think you are trying to instanciate a prefeb. Create a folder called "Resources" put the prefab into and call the function like this: Instantiate (Resources.Load(name), Vector3(r, 0, c), transform.rotation);

Comment
Add comment · Show 6 · 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 JVILL · Jan 31, 2013 at 07:40 AM 0
Share

Didn't work. I put the Terrain piece into a folder called 'Terrains' and used the code:

 function OnTriggerEnter ( other : Collider )
 {
 r = other.transform.position.x;
 c = other.transform.position.z;
 var name = other.gameObject.name;
 Instantiate (Terrains.Load(name), Vector3(r, 0, c), transform.rotation);
 }

Error says: BCE0005 $$anonymous$$ Identifier 'Terrains'. Thanks anyway though.

avatar image Loius · Jan 31, 2013 at 08:13 AM 0
Share

No, put the resource into a folder called 'Resources', like the answer said. Then use Resources.Load. :p

avatar image JVILL · Jan 31, 2013 at 08:29 AM 0
Share

I made a folder in the root Project directory called 'Resources'. I then put ALL of my Terrain pieces and renamed them so they would match with their 'name'. The code is:

 var name = other.gameObject.name;
 Instantiate (Resources.Load(name), Vector3(r, 0, c), transform.rotation);

Doesn't work. What have I missed :o

avatar image marius.treu · Jan 31, 2013 at 08:38 AM 0
Share

since you are using other.gameObject.name as resource-string. you have to be sure that the gameobjects name exactly is like the prefab you want to instanciate then it has to work no doubt.

If other.gameObject.name is an older instanciated prefab pay attention instanciating an prefab adds '(Clone)' to the end of your gameobjects name... example:

 GameObject new = Instantiate (Resources.Load("Something"), Vector3(r, 0, c) as GameObject;
 
 print(new.name); //will output 'Something (Clone)'
avatar image JVILL · Jan 31, 2013 at 08:56 AM 0
Share

Names are all exact. No errors, but they also don't instantiate. They aren't technically prefabs, they are Terrain pieces.

Also, the other.gameObject.name does not include (Clone) at the end of it's string because I manually label all of them.

Show more comments

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

12 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

Related Questions

Assigning a prefab a number suffix at runtime. 1 Answer

Instantiated rooms don't function properly. 1 Answer

[C#] How can I destroy instantiated prefabs when many are created with the same name? 2 Answers

How to create a clone of an object with a different name 1 Answer

Game Object seems empty after instantiation 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