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
0
Question by MKM1 · Apr 24 at 07:41 PM · spawning

Spawning enemies with Prefab script values

I just wanted to ask if you can Instantiate a Prefab with the prefabs own script values. I have a Character stat class in an enemy and I am trying to make the Stats go up when a spesific time is passed.

 public class Stat
     {
         public int BaseValue;
         private int Value;
 
     public int GetValue()
     {
         return Value;
     }

     public void SetValue(int value)
     {
         Value = value;
     }

     public void AddValue(int value)
     {
         Value += value;
     }
 }
   public class CharacterStats:MonoBehaviour
     {
         private int Health;
         private int CurrentHealth;
         public int BaseHealth = 100;
         public Stat Damage;
         public Stat Agility;
         public Stat Vitality;
         void Awake()
         {
             Damage.SetValue(Damage.BaseValue);
             Agility.SetValue(Agility.BaseValue);
             Vitality.SetValue(Vitality.BaseValue);
             Health = BaseHealth * Vitality.GetValue();
             CurrentHealth = Health;
         }
         void start()
         {
             
         }
 
         void update()
         {
         }
         public void TakeDamage(int damage)
         {
             CurrentHealth-=damage;
         }
 
         public int GetCurrentHealth()
         {
             return CurrentHealth;
         }
 
         public int GetHealth()
         {
             return Health;
         }
     }

but when I am Instantiating the prefab it seems like it has its own values is there a way to force the prefabs values to the Instantiated object.

 public IEnumerator SpawnEnemy()
     {
         while (true)
         {
             var index = Random.Range(0, Enemies.Count);
             Instantiate(Enemies[index], transform.position, transform.rotation);
             
             yield return new WaitForSeconds(2);
         }
     }
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
Best Answer

Answer by TreyH · Apr 24 at 08:16 PM

There are a few ways to go about this, but you might find it helpful to have some sort of Initialize function on your enemies for this purpose.

 public void Initialize() // <-- add whatever values you need
 {
     // Assign those values
 }


which you'd then call within your spawner thing:

 public IEnumerator SpawnEnemy()
 {
     while (true)
     {
         var index = Random.Range(0, Enemies.Count);
         var enemy = Instantiate(Enemies[index], transform.position, transform.rotation);
 
         // Provide your values here
         enemy.Initialize();
 
         yield return new WaitForSeconds(2);
     }
 }


For the "make stats go up" part, you may also want to consider keeping a reference to whatever was spawned. That will make it a bit simpler to modify whatever enemies are still alive after a certain amount of time etc.

Coroutines don't return values, but you can pass a callback for when certain events happen to help with these things.

 public IEnumerator SpawnEnemy(System.Action<Enemy> onEnemySpawned)
 {
     while (true)
     {
         var index = Random.Range(0, Enemies.Count);
         var enemy = Instantiate(Enemies[index], transform.position, transform.rotation);
 
         // Provide your values here
         enemy.Initialize();
 
         onEnemySpawned?.Invoke(enemy);
 
         yield return new WaitForSeconds(2);
     }
 }
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 MKM1 · Apr 24 at 08:57 PM 0
Share

Thank you for the answer ! I didn't know I could pass a callback in IEnumerator as well.

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

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

Having camera attack upon spawning player? 1 Answer

ai spawning please help 0 Answers

Multiplayer. Destroying player object on Host/Server 2 Answers

Check if bool is true then run script? 1 Answer

Help with Generating/Spawning Background Object 2 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