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 /
This question was closed Oct 27, 2016 at 03:56 PM by Aleksys for the following reason:

The problem was fixed by creating a prefab from the asset

avatar image
0
Question by Aleksys · Oct 26, 2016 at 05:46 AM · gameobjectinstantiateprefabassethierarchy

How to add an asset to a script-enabled public game object?

I'm following a tutorial that was created for Unity 4.6, and am up to a point where we are instantiating prefabs at runtime. The prefab to be created is added to the Inspector in Unity, with the codes public GameObject enemyPrefab and Instantiate (enemyPrefab, new Vector3 (0, 0, 0), Quaternion.identity);.The problem is that, with this being a GameObject, I'm unable to instantiate by using this unless an instance is already in the Hierarchy. So how do I add an asset to the position in the Inspector without adding it to the Hierarchy first?

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 hexagonius · Oct 26, 2016 at 08:41 AM 0
Share

That's a little confusing. By GameObject already in the scene you mean the one the script with your two code snippets is attached to? If so, of course it needs to be in the scene, otherwise it won't execute. But the code does not have to be on the prefab you want to instantiate as well. Usually you have two gameobjects, one in the scene with spawn code and one as a prefab referenced for instantiation.

avatar image Aleksys hexagonius · Oct 27, 2016 at 03:00 AM 0
Share

Sorry for the confusion. The script is attached to an empty game object that is in the hierarchy. The problem I have is that I can't add a prefab to it that I want to load at the start of the level unless an instance of the prefab is already in the hierarchy. To be clear, the reason I don't just add the object without a script is because this will be used to spawn and manage multiple of the same object.

Sorry for the trouble, the asset just wasn't being accepted as a prefab n.n'

1 Reply

  • Sort: 
avatar image
0

Answer by Light_ · Oct 27, 2016 at 03:08 AM

I think what Hex is getting at ^^ Put your script that calls the object to instantiate on an empty gameobject in the scene. Even if it has no renderer, something like a "gamemanager". Then use it to call the "instantiate(enemyPrefab) once the scene starts. If you're trying to "create an object from an instance" put a blank copy of your prefab with no renderer in the scene and see if that solves your problem?

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 Aleksys · Oct 27, 2016 at 03:38 PM 0
Share

I have an empty gameobject with the script attached, as suggested. Adding a blank copy to the scene does work, but, in the tutorial, the asset itself was added directly to the script by dragging the asset into the inspector of the empty gameobject, and that's what I am trying to replicate.

Sorry for the trouble, the asset just wasn't being accepted as a prefab n.n'

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 destroy all gameobjects active in hierarchy? 1 Answer

Instantiated Object being auto-deleted 0 Answers

How to instantiate within GameObject 1 Answer

Unity not Instantiating as Prefab 1 Answer

Save gameobjects prefabs from scene A to a list, then instantiate those game objects to another scene. 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