How to copy script to another GameObject
Hello. I have a base script which should contain the functionality for any script deriving from it to attach an instance of itself onto another GameObject.
For example: Base class is in script-X and both script-A and script-B are inheriting from it. script-X allows script-A to attach an instance of script-A to an already chosen GameObject, just as script-X also allows script-B to attach an instance of script-B to an already chosen GameObject.
Is this achievable and if so how could I do this? If this is not achievable do you have an idea as to how I should proceed?
More specific details of what I'm trying to do and what the problem is:
I'm making a 2D game. The game world is build up of tiles (square sprites next to each other). All tiles has a script attached to them (TileScr). A script is used for managing certain things (ManagerScr). ManagerScr has a 2D array of TileScr (called tiles) which contains all the tiles in order based on their position, so for example the tile at position (3, 8) can be accessed with Manager.tiles[3, 8].
Tiles can contain one type of plant at the time, though there are several types of plants each with their own behavior and properties. When a tile contains a plant an instans of that plants script is attached to that tile GameObject. Plants grows with time and when it has grown enough it will look for nearby tiles to spread to (all sorts of plants should work the same here). I found it easy to check for other available tiles by using
if (tileGameObject.GetComponent<PlantScr>() == null) {
//This tile is available.
}
since this works when the script inherits from PlantScr. My problem is that I find it difficult to get the plants to spread the right kind of plant since it wouldn't work by writing:
tileGameObject.AddComponent<PlantScr>();
since then it would attach PlantScr (the base script) to the tile GameObject instead of adding the correct script inheriting from PlantScr.
I've tried for quite some time without success. I've tried things similar to the following:
tileGameObject.AddComponent<typeof(this)>();
tileGameObject.AddComponent<GetType()>();
Answer by Harinezumi · Jan 30, 2018 at 03:28 PM
You need to make your PlantScr an abstract base class, and call the overridden function in the implementing type the actual adding. Something like this:
public abstract class PlantScr : MonoBehaviour { // PlantScr cannot be instantiated! Only its implementors can
protected abstract void SpreadTo (GameObject tileGameObject);
public void SpreadingCheck () { // can be called on any class deriving from PlantScr
...
if (tileGameObject.GetComponent<PlantScr>() == null) { // will find any script that derives from PlantScr
SpreadTo(tileGameObject); // will call the overridden implemented SpreadTo()
}
...
}
}
public class SpecificPlant1 : PlantScr {
protected override void SpreadTo(GameObject tileGameObject) { // do the same with the rest of your plant types
tileGameObject.AddComponent<SpecificPlant1>();
... // rest of your setup
}
}
Thank you very much! I hadn't yet considered PlantScr to be an abstract class and overriding functions to suit my needs. You helped me a lot here ^_^
Your answer
Follow this Question
Related Questions
Property Drawers and Inheritance 0 Answers
Inheritance Question 1 Answer
Referencing Base Class In Prefab 0 Answers
C# - creating a list of classes 5 Answers