Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 /
  • Help Room /
avatar image
0
Question by MidievalMan · Aug 09, 2020 at 12:32 PM · enemygetcomponentscriptableobjectscriptable objectenemies

How to Reuse Script on Enemies - Please Help!

Hello, I'm working on making a system where I use a scriptable object to store all the info that's unique to each enemy in my game. I want to make one reusable script that does everything every enemy has to do, with a derived script that has the reference to the unique scriptable object.

I'm stuck with trying to find a way to send this reference from the derived to the parent class because it seems like every way I could do it requires me to put in some unique code into the parent class such as GetComponent in which I would have to type a unique name for every enemy and thus make it a totally separate script which would be really bad for editing enemies.

Any suggestions on how I could get past this or how I could come up with a system that works better? if you need code or anything let me know, thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DialBlitzness · Aug 09, 2020 at 03:43 PM

Hello !

I may have a solution that comes close to what you want.

If I understood correctly, you want a global script which acts as a database for all the enemies loaded in your scene?

You can create a special scene called a "Loader", which will be the first loaded scene in your game, and which will only have the purpose of launching an associated script that will never get destroyed. Subsequently, the Loader loads the desired scene (The start of your game, or even a scene specified as a parameter), and the loader script will be accessible in all of your scenes.

 using UnityEngine;
 using System.Collections.Generic;
 using System;
 using UnityEngine.SceneManagement;
 
 [Serializable ()]
 public class Test : MonoBehaviour    
 {
     public string SceneToStart;
     public static string FirstScene;
     public static string  PrecedentScene  = string.Empty;
     public static string  CurrentScene  = string.Empty;
     public static Dictionary<string, DataCharacters> ListEnemies = new Dictionary<string, DataCharacters>();
 
     void Awake()
     {
         FirstScene = this.SceneToStart;
         PrecedentScene  = string.Empty;
         CurrentScene  = FirstScene;
 
         // Complete database when new game
         Database.PopulateDatabase(); 
         InitializeDatabase();
 
         DontDestroyOnLoad(gameObject);
 
            SceneManager.LoadScene(FirstScene); 
     }
 
     public static void LoadScene(string nextScene)
     {
         PrecedentScene = CurrentScene;
         CurrentScene = nextScene;
         SceneManager.LoadScene(nextScene); 
     }
 
     public void InitializeDatabase(){
         foreach(KeyValuePair<string, DataCharacters> character in Database.DataCharacters){
             ListEnemies.Add(character.Key, character.Value);
         }
     }
 }

In my example above, I create a list of enemies that I fill in InitializeDatabase (), thanks to a second class called "Database", which will contain all the necessary information. The "Database" itself is populate with "Database.PopulateDatabase() in Awake(), and this is the Database class :

 using System.Collections.Generic;
 
 /// <summary>
 /// Classe bibliothèque de données
 /// </summary>
 public class Database
 {
     public static Dictionary<string, DataCharacters> DataEnemies = new Dictionary<string, DataCharacters>();
 
     public Database (){}
 
     /// <summary>
     /// Remplir les données
     /// </summary>
     public static void PopulateDatabase()
     {
         PopulateDataCharacters();
     }
 
     public static void PopulateDataCharacters()
     {
         DataCharacters[1] = PopulateDataCharacter("Spider", 15, "A weak enemy");
 
         DataCharacters[2] = PopulateDataCharacter("Ghost", 10, "Boo !");
     }
 
     private static DataCharacters PopulateDataCharacter(string name, int hp, string description)
     {
         DataCharacters character = new DataCharacters();
         character.Name = name;
         character.hp = hp;
         character.description = description;
         return character;
     } 
 }
 

You can obviously add other lists or properties. You will have access to all the information of all the lists and data contained in the database and loaded into the loader, throughout your game!

Hope this helps you.

Comment
Add comment · 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
0

Answer by MidievalMan · Aug 09, 2020 at 05:31 PM

This helps, but I think it's a little too complicated for me to implement it into my game because I'm pretty new. I might look back and try a system like this in my next game! I hope this helps anyone else with a similar situation too.

Comment
Add comment · 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

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

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

Changing ScriptableObject to another during runtime crashes UnityEditor 0 Answers

How To Destroy/Deactivate one enemy without affecting other enemies of the same type? 0 Answers

ScriptableObject Life Cycle [ Help ] 0 Answers

Assign ScriptableObject (Inventory DataBase) 0 Answers

Scriptable Objects and Assetbundles 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