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 /
avatar image
0
Question by chibatribe · Jan 12, 2017 at 05:34 PM · variablegetcomponentclassreferenceinheritance

Is it possible to set variables for classes?

I'm basically making a single Game Controller script for many levels.The references of each level is stored in different classes like level 1,level 2 etc.So I need to refer these classes in my controller script .How can set a public variable that denote these classes so i can put that variable in the code

`"variable" reference = GetComponent<"variable">();

Comment
Add comment · Show 3
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 jmgek · Jan 12, 2017 at 06:16 PM 1
Share

@JoenigX3 explained how you can set a variable of type "FirstLeveLScript" and it's a good answer but I think since you're dealing with multiple levels you should look into inheritance since it can handle your question the best way.

 FirstLevelScript lvl1 = GetComponent<FirstLevelScript>();
 SecondLevelScript lvl2 = GetComponent<SecondLevelScript >();
 ThirdLevelScript lvl3 = GetComponent<ThirdLevelScript>();

You will want to do this:

 LevelScript lvl = GetComponent<FirstLevelScript>();
 lvl = GetComponent<SecondLevelScript>();
 lvl = GetComponent<ThirdLevelScript>();

And your class will look like this:

 public class LevelScript{
 ...
 }
 
 public class FirstLevelScript : LevelScript{
 ...
 }
 
 public class SecondLevelScript : LevelScript{
 ...
 }

Is this what you're asking?

avatar image chibatribe · Jan 17, 2017 at 11:34 AM 0
Share

@jmgek In my game I'm making separate level scripts for each level and i want a single game controller script. which take in reference from current level script which is one from many.I set currentlevel in player prefs so controller can understand which level.Basically I want to put the same controller script for every level and this controller script invoke values from the level script. The problem is where I put reference in the controller for the current "level script".I created level scripts like Level 1,Level 2,Level 3,etc,.... As i put the same controller in every scene the refference in that script should change for example: If it's level 1 it'll be Level1 currentlevel=GetComponent(); and as I put the script in level2 it should change to Level2 currentlevel=GetComponent(); and so on. Currently Im making separate controller scripts by copy pasting the same script but changing the above code. Hope u can help me thanks in advance :)

avatar image jmgek chibatribe · Jan 17, 2017 at 06:42 PM 0
Share

I am really sorry it's very confusing as to what you're trying to accomplish from that response. What I understand is you want to set one property of type 'Level' in your character controller.

 class character
 {
     LevelScript levelScript;

    void Start()
    {
         if(isFirstLevel)
              levelScript = GetComponent<FirstLevelScript>();
         if(isSecondLevel)
              levelScript= GetComponent<SecondLevelScript>();
         if(isThirdLevel)             
               levelScript= GetComponent<ThirdLevelScript>();
    }
 }
 

If you re read what I posted above you can easily assign any level script to 'levelScript' using Class inheritance. As long as each level class inherits off a base level class.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KoenigX3 · Jan 12, 2017 at 05:40 PM

Use the name of the class as the type of the variable. After you have compiled your Game Controller class (which is, for example the LevelScript):

 FirstLevelScript lvl1 = GetComponent<FirstLevelScript>();

This 'lvl1' variable will return the script named FirstLevelScript on the GameObject.

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 chibatribe · Jan 12, 2017 at 07:32 PM 0
Share

Thanks man, but it's not what I actually need .It's like a Single variable like "CurrentLevel" in the code where Class name should be.So it automatically refers to that level.

`"CurrentLevel" something = GetComponent<"CurrentLevel">();

avatar image jmgek chibatribe · Jan 12, 2017 at 07:42 PM 0
Share

Sorry for jumping on $$anonymous$$oenigX3 answer but you're not making a lot of sense. Above I posted how you can create a variable that would hold the classes and you could just reference the one you needed. You would set the level that you're currently on.

 FirstLevelScript lvl1 = ...; 
 lvl = ...;

You can't "Automatically" refer to a class. Could you please explain a little better.

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

6 People are following this question.

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

Related Questions

Private variable is not saved 3 Answers

Parent Class variables in Child Class's Inspector (C#) 0 Answers

How to build up the class structure for an object 1 Answer

Can I make reference/assocation to another component as variable? 0 Answers

Enemy variable remains null 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