- Home /
How do I change public variables depending on which level?
I have this script AnimalContainer:
public SheepFunctions a1;
public BirdFunctions a2;
public CowFunctions a3;
public PigFunctions a4;
void Start () {
a1 = gameObject.AddComponent<SheepFunctions>();
a2 = gameObject.AddComponent<BirdFunctions>();
a3 = gameObject.AddComponent<CowFunctions>();
a4 = gameObject.AddComponent<PigFunctions>();
}
//ANIMAL 1
public void Animal1Shape()
{
a1.ChangeShape();
}
public void Animal1Update()
{
a1.UpdateFunctions();
}
public void Animal1Ability()
{
a1.Ability();
}
These are the circumstances I want for level1 (scene 1).
In level2 I would like the public variables and the Start() content to be changed to the following:
public FoxFunctions a1;
public TurtleFunctions a2;
public DinoFunctions a3;
public HorseFunctions a4;
void Start () {
a1 = gameObject.AddComponent<FoxFunctions>();
a2 = gameObject.AddComponent<TurtleFunctions>();
a3 = gameObject.AddComponent<DinoFunctions>();
a4 = gameObject.AddComponent<HorseFunctions>();
}
Also, I can't just make another script for each level. Because this class is loaded from the PlyaerController, like this:
ac = gameObject.AddComponent<AnimalContainer>();
How can I solve my problem, when I can't make "if-statements" at the variable declaration section? Been speculating all day now!
EDIT: Is it possible to convert a type, such as C# class into another C# class, because that might do it. Something like:
a4 = a4 as HorseFunctions;
I know this doesn't work, it says, Cannot convert type PigFunctions' to
SheepFunctions' via a built-in conversion. But I hope you get the idea?
Answer by _travistx · Aug 18, 2014 at 04:31 AM
It sounds like you would benefit from making your classes inherit from an interface. Something like this:
public interface IAnimalFunctions
{
void ChangeShape();
void UpdateFunctions();
void Ability();
}
Then in each of the animal functions, inherit from this interface:
public class SheepFunctions : IAnimalFunctions
{
public void ChangeShape()
{
}
public void UpdateFunctions()
{
}
public void Ability()
{
}
}
And then in your AnimalController, you can do this:
public IAnimalFunctions a1;
public IAnimalFunctions a2;
public IAnimalFunctions a3;
public IAnimalFunctions a4;
void Start () {
a1 = gameObject.AddComponent<SheepFunctions>();
a2 = gameObject.AddComponent<BirdFunctions>();
a3 = gameObject.AddComponent<CowFunctions>();
a4 = gameObject.AddComponent<PigFunctions>();
}
I hope this helps. If you'd like to know more, you might want to do some reading about object oriented programming.
When I do exactly as you described here. I get the following error: Assets/Scripts/AnimalContainer.cs(16,33): error CS0309: The type SheepFunctions' must be convertible to
UnityEngine.Component' in order to use it as parameter T' in the generic type or method
UnityEngine.GameObject.AddComponent()'
I will try to read up on inheritance
Ah ok, fixed it now. Thanks for this great tip.
Just had to change this: public class SheepFunctions : $$anonymous$$onoBehaviour, IAnimalFunctions {