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!
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.
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.
Your answer
Follow this Question
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