Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by bbaarrr2015 · Nov 11, 2015 at 04:33 PM · unity 5script.unity5duplicate

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bbaarrr2015 · Nov 13, 2015 at 05:40 PM 0
Share

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 ();
     }
 }

@pekalicious

avatar image pekalicious bbaarrr2015 · Nov 13, 2015 at 05:50 PM 1
Share

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 ();
      }
  }
avatar image bbaarrr2015 pekalicious · Nov 13, 2015 at 08:50 PM 0
Share

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 :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Master Audio source volume control with slider 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges