• 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 /
This question was closed Aug 17, 2014 at 08:43 PM by GimLee for the following reason:

Seems impossible

avatar image
0
Question by GimLee · Aug 17, 2014 at 06:01 AM · c#variabletype

How to declare the type after a string?

I need to do something like this:

 private string stringValue = "SheepController"; //SheepController is the name of another script
 private **stringValue** animal1;

So it would be like writing:

private SheepController animal1;

Is this even possible? Because it will differ from each level in my game, what script I would like to add to a gameobject.

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 robertbu · Aug 17, 2014 at 06:14 AM 0
Share

Not possible.

avatar image GimLee · Aug 17, 2014 at 06:15 AM 0
Share

For sure?

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Aug 17, 2014 at 06:26 AM

Not sure of your game, but one common way is to declare a base class with a common interface and then derive the other classes from it. There are other solutions. Here is one question that talks about the issue:

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

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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

Related Questions

C# - passing a Type var to GameObject.AddComponent 1 Answer

Is it possible to use a variable as a Type? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Vector3 is a 'type' but it is used like an 'variable' (C#) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges