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 techdoctorllc · Nov 10, 2020 at 02:14 PM · variablesreferencing

Storing script names in a variable

Hi, new to unity so please ignore if I sound stupid.

I have to scripts references in a script. Example, script A and B are referenced in script C.

I need to store script A and B variable names in a another variable so that I can use that variable in conditioning.

 private FemalePlayerAnimations femalePlayerAnimations;
 private MalePlayerAnimations malePlayerAnimations;

 private Variable variable; // Got Problem Here


 void Awake()
 {
     femalePlayerAnimations = GetComponent<FemalePlayerAnimations>();
     malePlayerAnimations = GetComponent<MalePlayerAnimations>();
 }

 void Start()
 {
     if(1 + 1 = 2) // Some Condition
      {
         variable = femalePlayerAnimations;
      }
     else if(1 + 2 = 3) // Some Another Condition
      {
         variable = malePlayerAnimation;
      }
 }

Thanks in advance.

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

Answer by jackmw94 · Nov 10, 2020 at 02:57 PM

This is a really good question for you to have asked because I think there are a few interesting takeaways from this answer!

Firstly, since you have two different classes that will both provide you with a common service (Female/MalePlayerAnimations, presumably will both provide you with a set of clips or something similar) this is a great place to use either an interface or superclass.

 public interface class I_AnimationProvider
 {
     void GetAnimations(); // or whatever you do in both male + female animations scripts
 }
 
 public class FemalePlayerAnimations : I_AnimationProvider
 {
     public List<AnimationClip> GetAnimations()
     {
         return femaleAnimations;
     }
 }
 
 public class MalePlayerAnimations : I_AnimationProvider
 { 
     public List<AnimationClip> GetAnimations()
     {
         return maleAnimations;
     }
 }

The above shows how both animation scripts can implement an interface. This means you know any object that implements I_AnimationProvider will have a function called GetAnimations.

 public enum AnimationSet
 {
     Male,
     Female,
     // Animal, etc..
 }
 
 [SerializeField] private AnimationSet animationSet;
 
 private FemalePlayerAnimations femalePlayerAnimations;
 private MalePlayerAnimations malePlayerAnimations;
 private I_AnimationProvider ourAnimationProvider;
 
 private void Awake()
 {
     femalePlayerAnimations = GetComponent<FemalePlayerAnimations>();
     malePlayerAnimations = GetComponent<MalePlayerAnimations>();
 }
 
 private void Start()
 {
     if (animationSet == AnimationSet.Female) // Some Condition
     {
         ourAnimationProvider = femalePlayerAnimations;
     }
     else if (animationSet == AnimationSet.Male) // Some Another Condition
     {
         ourAnimationProvider = malePlayerAnimation;
     }
 }

Now you can ignore the male / female animation scripts that you found and use only the animation provider to access your animation data. I've added an enum to this script as this will dictate which type of animation you use, think of this as a nicer way of having a bool saying 'usingMaleAnimations'. It's nicer because if you decide you want more animation types then you can add a new element to the enum and a new else-if statement to the start function checking for that new enum element.


You can do a pretty similar thing by swapping out the interface for an abstract class, this would mean both Male/FemalePlayerAnimations inherit from the same type. You can then have a single variable just called PlayerAnimations and it will find either the male or female version - just make sure the gameobject only has the desired one added to it only!



This is how I'd modify your current code to work as you expect and if you're happy with this feel free to stop reading here. However, I think there is a much simpler way to do this: (don't mix up code below with code above, they're two different ways to do this!)

ScriptableObjects are a way of making persistent data assets. Instead of making them in the scene you will make them in the project and hold various settings / configs / data. I'm thinking that you could have a scriptable object class that stores a list of animations, then you can do away with both male/female player animation scripts and instead of containing a reference to either you have a reference to your scriptable object which holds the animations.

 [CreateAssetMenu(menuName = "Create PlayerAnimationData")]
 public class PlayerAnimationData : ScriptableObject
 {
     // whatever data your male/female classes used to hold, animation clips maybe?
 
     public AnimationClip[] Animations;
 }

You can use this class, then in Assets -> Create menu you should see "Create PlayerAnimationData appear! You can make two, one for male and one for female then instead of having to worry about checking about what setting you've set, you simply use one of these objects.

 [SerializeField] private PlayerAnimationData animationData;
 
 private void Awake()
 {
     // not needed!
     //femalePlayerAnimations = GetComponent<FemalePlayerAnimations>();
     //malePlayerAnimations = GetComponent<MalePlayerAnimations>();
 }
 
 private void Start()
 {
     // not needed! - use your referenced animationData instead
     //if (animationSet == AnimationSet.Female) // Some Condition
     //{
     //    ourAnimationProvider = femalePlayerAnimations;
     //}
     //else if (animationSet == AnimationSet.Male) // Some Another Condition
     //{
     //    ourAnimationProvider = malePlayerAnimation;
     //}
 }
Comment
Add comment · Show 1 · 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 techdoctorllc · Nov 11, 2020 at 04:58 AM 0
Share

Thank you so much for such a great and in detailed information. For me it will take some time to understand and implement as I have just put my hands on C Sharp (I am an intermediate level java android app developer). But yes, your solution looks promising.

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

140 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reference specific variable from a specific gameObject? 2 Answers

Referencing AND affecting a variable from another script 1 Answer

Getting a variable from a specific gameobject for another script 3 Answers

How can I reference a float variable to a GUI label in another script? 1 Answer

Properly referencing a variable from another script 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