Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by kebrus · Mar 26, 2013 at 11:20 AM · gameobjectinstantiateprefab

How to tell a prefab to instantiate itself?

hi! i have this enemy that has a certain number of lives, each time it dies i want it to respawn until it runs out of lives, it's a complex enemies full of properties and states so i can't simply give him full health again.

i thought i could simply keep a reference on itself OF itself, so lets say prefab type ENEMY has a reference for ENEMY, so when it dies i create a new one using that reference, the problem is, the reference is cyclic, because it's referencing itself when i instantiate a new enemy using that reference i'm actually spawning the same enemy, so all the states and properties come with it, this happens because the prefab is referencing itself, but if i don't use that reference in the prefab and simply change it on a already placed enemy, now the new enemy is an instance of the prefab, BUT since now i don't have the reference in the prefab the new enemy borns without it so i lose my reference and i can't respawn it again

how can i assign a reference for a prefab in script or how should i solve my problem?

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 whydoidoit · Mar 26, 2013 at 11:23 AM 0
Share

$$anonymous$$aybe I'm missing something - but when it dies, why don't you just move it back to the spawn point? $$anonymous$$aybe after a period of deactivation - you certainly don't want to be using Instantiate when you don't have to...

avatar image kebrus · Mar 26, 2013 at 12:27 PM 0
Share

i simply can't, the enemy is complex, it loses parts, creates stuff and jumps from states to states, i really need to create a new one with all the initial conditions

avatar image liszto · Mar 26, 2013 at 12:39 PM 0
Share

Why you don't reset it to its initial condition ? It's the best solution. I approve the whydoitdoit solution and it's the more optimized.

avatar image Tasty Risotto · Mar 26, 2013 at 12:56 PM 0
Share

I'm quite lost.. $$anonymous$$aybe this? class Enemy { public GameObject EnemyPrefab; ... ... Die() { ... GameObject newEnemyGO = Instantiate(EnemyPrefab) as GameObject; Enemy enemy = newEnemyGO.GetComponent(); enemy.EnemyPrefab = EnemyPrefab; Destroy(gameObject); } }

avatar image kebrus · Mar 26, 2013 at 02:17 PM 0
Share

@tasty

that would work, but that way you are referencing itself and the new instance gets the same properties of the the actual state of the prefab, and not it's initial settings

1 Reply

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

Answer by whydoidoit · Mar 26, 2013 at 12:38 PM

You could load the prefab using Resources.Load and putting it in a Resources folder - then all you need is its name, rather than an object reference.

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 kebrus · Mar 26, 2013 at 02:07 PM 0
Share

that actually works, thx

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

13 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

Related Questions

How to instantiate within GameObject 1 Answer

Referencing gameObject from script after Instantiate 0 Answers

autolink to prefab? 0 Answers

How many time does a prefab takes to load?? 1 Answer

Instantiate as a child on a certain point? 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