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 jjplay175 · Jun 11, 2015 at 09:24 PM · c#stringcomponent

GetComponent(); issue

Hi so I have a script that I want to handle all interactions so when a mouse clicks something it easily knows where to go from there

The idea is that you on click the raycast finds the interaction script and then in the interaction script it will have a variable which is set Per Object as to what script it will activate, Example : If the interaction script is on a spawner the raycast will activate the interaction script and then the interaction script will activate the Spawn script

This script is obviously incomplete and is the bare minimal to show where I am heading with the code

     public bool Active;

 public string ScriptName;
 public bool RequiresRaycast;

 void Update(){
     if (Active == true) {

         UnknownScript = transform.parent.GetComponent<ScriptName>();
         UnknownScript.Active = true;

         Active = false;

Is there a way I can get this to use a string in .GetComponent? It seems quite complicated considering you need to make it a variable itself when you get it so it would mean I would have to make it literal when I write it like "ScriptName = transform.parent.GetComponent();" the first part "ScriptName" would have to be a new variable using the strings variable and not to try and reset it... God knows

Ps: the RequiresRaycast variable will be implemented later for when script clicked needs the information of where it was hit, example : Movement on terrain

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 FortisVenaliter · Jun 11, 2015 at 09:26 PM 0
Share

Is there a question included here?

avatar image jjplay175 · Jun 11, 2015 at 09:40 PM 0
Share

Sorry updated the question to be more clear

1 Reply

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

Answer by zach-r-d · Jun 11, 2015 at 09:36 PM

If ScriptName is a string, you must use the non-generic version of GetComponent, and treat it as a MonoBehaviour:

 MonoBehaviour UnknownScript = transform.parent.GetComponent(ScriptName) as MonoBehaviour;

I also recommend using .enabled rather than defining your own .Active as .enabled will probably do what you want already.

Comment
Add comment · Show 4 · 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 jjplay175 · Jun 11, 2015 at 09:39 PM 0
Share

Yeh I think I understand what you mean here, I will probably have to do a little bit of research and a bit of tinkering but this should be able to work somehow, will keep you posted

And as for the .enable, good call, I will probably switch it out to that

avatar image zach-r-d · Jun 11, 2015 at 09:42 PM 0
Share

So you want to find all of the scripts of a given type in the scene and enable them? Or perhaps all of the scripts of a given type in just the parent?

avatar image jjplay175 · Jun 11, 2015 at 09:44 PM 0
Share

$$anonymous$$y Original reply to you was kind of meant for the other guy sorry, I edited it

Do you feel it would be just as efficient for me to just have several functions in my single Interaction script and then on Interaction "Start" it invokes 1 of the functions using bools for each? then I can just have in the Functions to get component of that specific script and activate it in the parent?

Example, Void Script1, Void Script2, Void Script3 <-- Script1 might get a spawn script component and activate it if bool1 = true?

avatar image jjplay175 · Jun 12, 2015 at 12:18 PM 1
Share

I just went with using the new method by using multiple bools and the script activating it, thanks for the help though! I'm sure other people will find your answer useful

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

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Unity can't see any scripts 0 Answers

how to disable/enable a script in c#? 2 Answers

CinemachineBrain component reference 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