Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by GimLee · Aug 17, 2014 at 11:09 PM · variableclassdeclaration

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?

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
1
Best Answer

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.

Comment
Add comment · Show 2 · 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 GimLee · Aug 18, 2014 at 06:52 PM 0
Share

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

avatar image GimLee · Aug 18, 2014 at 06:57 PM 0
Share

Ah ok, fixed it now. Thanks for this great tip.

Just had to change this: public class SheepFunctions : $$anonymous$$onoBehaviour, IAnimalFunctions {

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

24 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

Related Questions

Declaring Variables in an Array of a Class 0 Answers

Is it possible to set variables for classes? 2 Answers

C# bug with a class boolean ? 1 Answer

Creating A Class Variable That Works With Any Class 0 Answers

Can I choose what to pass into .getcomponent<{VARIABLE}>(); 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