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
1
Question by Essential · May 26, 2013 at 04:14 PM · variablereferencetypegamecontroller

How can you store a reference to something that you don't know the type of yet?

I have a variety of different game modes, and I'd like to spawn a different GameController script at the beginning of the scene depending on the game mode selected. Then other mini-managers (e.g., SpawnController), would reference the main GameController, whether that be GameController_Mode1, GameController_Mode2, etc. But how can I have other objects referencing this if I don't know the type?

Comment
Add comment · Show 2
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 JCprogrammer · May 26, 2013 at 04:25 PM 0
Share

can you be more specific please ? i couldn't figure out what is the issue about ...

avatar image Essential · May 26, 2013 at 04:28 PM 0
Share

I might start the level spawning either the script 'GameController_Timer', or 'GameController_Unlimited', or 'GameController_Collect', etc. and I want other gameObjects to be able to reference whichever GameController component is spawned to send information to it, grab information from it, and so on. But I have to declare the variable type in these other scripts… so I can't see how to make this work.

2 Replies

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

Answer by Essential · May 27, 2013 at 03:27 PM

So after a little searching and thanks to a great answer from Jerdak on Stackoverflow, I've found the Javascript / Unityscript answer is in Polymorphism, or basically the use of script inheritance. Here's a basic example for anyone who might find it useful. We create these three following scripts and create an empty GameController gameObject then add the 'GameController' script to it, as well as the 'Controller_SurvivalMode' script.

BaseController:

 var c : String;
 
 function WhatControllerIsThis()
 {
     c = "I am the Base Class";
     Print( c );
 }
 
 function Print( d : String )
 {
     Debug.Log("This controller is: " + this);
     Debug.Log(d);
 }

Controller_SurvivalMode:

 class Controller_SurvivalMode extends BaseController
 {
     function WhatControllerIsThis()
     {
         c = "Today I will be a survival controller";
         Print( c );
     }
 }

GameController:

 var controller : BaseController;
 
 function Awake()
 {
     controller = GetComponent(BaseController);
     controller.WhatControllerIsThis();
 }


Running this results in printing:

 "This controller is: Controller_SurvivalMode"
 "Today I will be a survival controller"


Usually you couldn't store a reference to one script of under another type of script, but because of the extends keyword, the 'Controller_SurvivalMode' script is treated as a BaseController script through inheritance. Whatever different game mode you want, attach that 'Controller_[Mode]' script to the GameController GameObject, and so long as it extends the BaseClass, you'll only ever need to typecast it as the BaseClass! :)

Comment
Add comment · 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
1

Answer by robertbu · May 26, 2013 at 04:32 PM

Interfaces, abstract classes, or a base class with an override. Take a look at the answer by @asafsitner in this question:

http://answers.unity3d.com/questions/381529/how-to-address-scripts-without-knowing-their-names.html

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 Essential · May 27, 2013 at 03:35 PM 0
Share

Thanks for the answer robertbu. It was very helpful but I noticed those options were mostly C# and I'm using Javascript unfortunately. The base class with overrides worked, but I found the explanation by asafsitner a bit complicated for my understanding, but I got my head around it eventually! :)

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

15 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

Related Questions

Use Of Static Variables 1 Answer

C'# When Taking Variables From Other Scripts, The Values Do Not Update 1 Answer

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

Turn Strings[] into vars[]? 1 Answer

Setting variable as type "Scene" 4 Answers


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