- Home /
How to add multiple scripts to a script? (C#)
Okay, so, I have a system set up where I have several types of triggers in my game (they all extend Trigger
), and are attached to an object as components. However, I have come upon a situation where I would like to add multiple triggers. I came up with an Idea, but I can't get it to work.
Basically, I want to have a wrapper class, we'll call it MultiTrigger
, that has a public List
element. That works fine, but I can't add the triggers to the list in the inspector as they are just scripts and not GameObject
s. Is there a way to do this? I would prefer to bot create a bunch of empty GameObject
s to attach them to.
Not sure if I get a good enough overall view of your situation and what you'd like to do.
Does Trigger
have to be a $$anonymous$$onoBehaviour? If it does, it has to be attached to a GameObject anyway so you might as well create some (or one and add multiple Trigger scripts to it).
I can't add the triggers to the list in the inspector as they are just scripts and not GameObjects
Could you make a prefab with a Trigger added to it and drag that ins$$anonymous$$d?
If you make a public Trigger[]
or any kind of list for the triggers, the items that go into the list need to be initialised in some way ( you need to create instances of Trigger class). This can be done in 3 ways basically
Dragging scene objects into the list (scene creation Instantiates the objects and the list gets a reference)
Calling Instantiate() manually.
triggerList.Add(Instantiate(triggerPrefab) as Trigger)
If Trigger is not a $$anonymous$$onoBehaviour, you can just do
triggerList.Add(new Trigger())
Answer by hexagonius · Feb 25, 2015 at 09:07 AM
I think what you want to achieve is not possible out of the box. Maybe this helps, just had a quick look:
http://forum.unity3d.com/threads/serializable-system-type-get-it-while-its-hot.187557/
Answer by PokeJoe · Feb 25, 2015 at 09:18 AM
Generally what I do in this situation is, for each instance you need of the script, make a new empty game object and add one instance of that script. Then, instead of attaching the scripts directly to your master script, attach the dummy game objects instead and access them that way. You can parent them to the main object if that makes it easier.
Your answer
Follow this Question
Related Questions
GameObject is already being activated or desactivated 2 Answers
How to make an object invisible before it is triggered to show? 2 Answers
One script for many Game Objects with different values 0 Answers
How to detect an object which be in FOV of certain camera ? 1 Answer
How Can i Fade In a GameObject with LeanTwean plugin? 1 Answer