- Home /
Make an array of scripts?
Hey guys! I know this sounds strange, but i have been working on an RPG that i am planning to release as a complete project for beginners to have a base. I am trying to make everything as compact, performance efficient and easy to understand as possible, but i am having a problem with making something.
I want to make a SINGLE SCRIPT that will be some kind of Quest Manager. Everytime the player talks about a specific thing with a specific NPC, the game is supposed to notify this Quest Manager with an ID. The Quest Manager will then proceed to verify if he has such ID on a list, and then activate a specific quest script based on the list's number.
Problem is, as far as i know, the only way to reference a script is to use the script's name as a type itself, so i can't just code something like public C#Script[] database
. So what am i asking is, Is there any way that i can make an array of C# Scripts? Like the normal arrays that you can make with GameObjects or AudioSources for an example. If there is, please explain how would i do it or link a tutorial or something. Any help would be appreciated, thanks!
Answer by Reynarz · Aug 14, 2017 at 10:05 PM
There are many ways of do it:
Way 1: using an interface and that all your classes implement it, and make a list of that interface:
List<IMyInterface> _theClassesThatImplementsThatInterface;
Way 2: your classes are inheriting from MonoBehaviour?
List<MonoBehaviour> _theClassesThatInheritsFromMonoBehaviour;
Way 3: Your classes inherit from another class(is the same example that above)
List<MyBaseClassType> _theClassesThatInheritsFromMyBaseClassType;
For populating the list, is like always.
Way 1:
_theClassesThatInheritsFromMonoBehaviour = new List<MyInterface>()
{
new MyClassThatImplementTheInterface1() { life = 2, name = "hello"},
new MyAnotherClass() { hour = 3, minutes = 4, seconds = 7}
}
Way 2
_theClassesThatInheritsFromMonoBehaviour = new List<MonoBehaviour> ()
{
new MyClassThatInheritsFromMonoBehaviour() { Hello = "hola", Goodbye = "Adios"},
new MyClassAnotherClass() { Casa = Home, Carro = "Car"}
}
Way 3: the same way of the way 2, but with the class name.
And if you want populate the list Line by line:
_theClassesThatImplementsThatInterface.add(new MyClass { Blah = "Heeey"} );
That ways are for scripts of multiples types, but if you only want one:
public MyScript[] _myScript;
private void Start()
{
_myScript = new MyScript[/*Array Length*/];
}
private void PopulateArray()
{
_myScript[0] = GetComponent<MyScript>();
}
Cool, works like a charm. A few more AI fixes here and there, a little more polishment, and the project will be ready in no time! ^.^
Great!!,@Bryangs $$anonymous$$mm what example you choose it?
I'm sorry, looked at example no 4, but i wonder about PopulateArray(), would hat also go in the Start? Just looking at possible ways to populate arrays of scripts that are part of different objects, while the script itself is the same.
Your answer
Follow this Question
Related Questions
Test All Variables in an Array C# 2 Answers
How can I use the same script on multiple objects without conflicting variables. 2 Answers
Initiate class array at start and assign class values. 0 Answers
Add two radius on the same shader 1 Answer
How to make an array with different variables and visible in Unity's inspector 1 Answer