Duplicate script
ok so i build a game and its common when i attach the same script to the same object but on another level i want to change the code abit, there is a way to create kind of "prefab" to a script? i hate duplicating the script its make alot of mess.. Thanks for helpers.
Answer by pekalicious · Nov 12, 2015 at 04:58 AM
It's called inheritance and it goes something like this:
(WARNING: super oversimplified explanation)
public class MyParentClass
{
protected int aNumber;
public void DoSomething()
{
SetNumber(10);
}
protected void SetNumber(int newNumber)
{
aNumber = newNumber;
}
}
public class MyChildClass : MyParentClass
{
public void DoSomethingElse()
{
SetNumber(200);
}
}
public class MyOtherChildClass : MyParentClass
{
public void OverNineThousand()
{
SetNumber(9000000);
}
}
In the above very basic example, we have a single MyParentClass script that has a number, a method that sets that number and its own DoSomething method. Then we have 2 other classes that inherit the parent class (": MyBaseClass"). They have their own methods of doing something, but they all call the same SetNumber method.
Even though the child classes are not defining aNumber and SetNumber, they are both inherited by the parent. Basically you are saying "Make a class with these members, and also whatever members my parent has so I don't have to write the same thing over and over".
Note that for the child classes to have access to a parent member, it has to either be protected or public. If its private, you are still inheriting it, but you cannot access them.
Also note that each class has a different method that does something (DoSomething, DoSomethingElse, etc). So to perform each task, you need to call those methods as so:
MyParentClass parent = new MyParentClass();
parent.DoSomething();
MyChildClass child = new MyChildClass();
child.DoSomethingElse();
MyOtherChildClass otherChild = new MyOtherChildClass();
otherChild.OverNineThousand();
You can also use the same method name and override its behavior in the subclass, but I'll leave you to read the tutorial which explains that.
But i can't use any function correctly, this one from parent Enemey;
public void TinyBlocker$$anonymous$$ovesRL(){
rb.velocity = new Vector2 (velo, rb.velocity.y);
cx = Blocker.transform.position.x;
cy = Blocker.transform.position.y;
if (count == 1) {
cy*=-1;
if(velo > 0){
velo+=getfaster;
}
ChangePos(Blocker,cx,cy);
count = 0;
}
}
this is the Child code;
using UnityEngine;
using System.Collections;
public class TinyLRL3 : Enemey {
// Use this for initialization
Enemey tiny = new Enemey();
void Start () {
}
// Update is called once per frame
void Update () {
tiny.TinyBlocker$$anonymous$$ovesRL ();
}
}
What exactly are you trying to do? Execute the same block of code, or grab another enemy and execute HIS code?
If you are just trying to execute the same code, then you don't need to define enemy, because TinyLRL3 IS an Enemy, so it does have the TinyBlocker$$anonymous$$ovesRL, even though you don't see it in the class. So, you simply do this:
using UnityEngine;
using System.Collections;
public class TinyLRL3 : Enemey {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
TinyBlocker$$anonymous$$ovesRL ();
}
}
Thank you my friend ! , you did my day! well its better to do to each level a child ins$$anonymous$$d to copy it hundred time :)
Your answer
Follow this Question
Related Questions
How to reference script in unity (C#) ? 0 Answers
How to assign Instantiated object through script? 1 Answer
How do i put "Wasted!" after respawn? 0 Answers
Rts game. 2d unit on 3d scene 0 Answers