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 Demmy · Aug 23, 2014 at 10:08 PM · c#

Making script object without declaring script name

I want to access a non-specific script. That script's name depends on the object's tag;

for example if the tag of the object that holds the script is "Door1", script's name will be "interactScriptDoor1" and if the tag is "LockedDoor", the script's name would be "interactScriptLockedDoor". Note that these scripts are already made and named.

I'm attempting to call these scripts from another script on another object, which works fine. The problem is that I have to make them all separately: I always have to declare the type by the name of the script: For example:

 private interactScriptDoor1 launchItem1;
 private interactScriptLockedDoor launchItem2;
 ...

and then:

 launchItem1=targetedObject.GetComponent<interactScriptDoor1>();
 launchItem2=targetedObject.GetComponent<interactScriptLockedDoor>();

The point is that every object has only one interact script; What I wish to achieve is something like

 private Script launchItem; //script not being any existing script(only type)

and declare it with:

 launchItem=targetedObject.GetComponent("InteractScript"+targetedObject.tag);

The problem is that I can't declare launchItem without declaring it with specific script in the first place. If anyone wonders why would I want this, it's for avoiding constantly updating interaction script when new types of object appears. Please help.

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

2 Replies

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

Answer by robertbu · Aug 23, 2014 at 10:14 PM

You cannot get where you want to go with this approach. That is, it is possible to accessing objects generically as a Object class or perhaps more useful, a MonoBehaviour. And you can use the string form of GetComponent() of get the component. The problem is that the string from returns something of type Component. So you would need to cast the resulting reference to some type. To make that happen, you would have to know the type. And that kinda defeats the purpose of what you are trying to do. Here are the commonly suggested (on UA) approaches to solve the problem I think you are trying to solve:

SendMessage()

Having a base class and deriving the individual enemy behavior by overriding.

Abstract Classes

Interfaces

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
0

Answer by Kiwasi · Aug 23, 2014 at 11:58 PM

While technically possible, this is a bad idea.

Consider alternative ways to structure your code.

  1. Use inheritance. Have a parent script called interaction and have door1 and locked door inherit from it. Implement any common functionality on the interaction script. Then you can use get component on the interaction script, and get the appropriate script.

  2. Use interfaces. Result is almost identical, though you have to do some trickery with GetComponent

  3. Use send message to achieve the same functionality on different components. Main downside here is typos are really easy

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

I'm having more problems accessing a variable from another script in c# 2 Answers

How to pass array variables from one script in one scene to another script in another scene? 2 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