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 superventure · May 19, 2012 at 12:38 AM · inputvariableinspectorgetcomponentstring

GetComponent from string name?

is it possible to make a GetComponent command read a string variable for another script?

ie

var CurMission: String; function Start(){ if(player.GetComponent(MissionScript)...CurMission... == true){ //do something }

This is going on a more detailed script I've made for npcs. I am going to have multiple missions with many different variable names- I would like to be able to input those specific variable names through the inspector, if possible.

any help is appreciated, thanks

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

3 Replies

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

Answer by shadowriffe · May 19, 2012 at 01:34 AM

I don't think you can overload GetComponent with a string, so having it fill that in at runtime would not work.

Here is a snippet of code that might help depending on what you need to do,which I can't really understand, and I know it's not super efficient. It uses a string to search the heirarchy of a tranform to look for a transform with a specific name.

static function FindTransform(parent : Transform, name : String) : Transform{ if (parent.name == name) return parent; var transforms = parent.GetComponentsInChildren(Transform); for (var t : Transform in transforms) { if (t.name == name) return t; } return null; }

So I could see you have a GameObject in a character called Mission_FetchQuest2 and your character could have a list of strings that denote active quests, you could look for "Mission_FetchQuest2" in the quest-giving NPC, and it would return the corresponding transform/object.

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 superventure · May 19, 2012 at 01:56 AM 0
Share

That is a good idea, but the initial mission script variables are all booleans. I'm trying to get the npcs to check for a specific boolean in that script, so that if it is false, they say or do specific things until that boolean is true. this method would allow me to use one universal script for npcs because I would be able to easily name which variable to check for in the inspector

avatar image
1

Answer by AchillesWF · May 19, 2012 at 01:33 AM

Sure you can. See this:

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Just don't pile up the functions too heavily in a single expression without checking the return value to make sure it isn't null, or else prepare to do a lot of work tracking down null references! Once you have an object, you can get at any of its components, but make sure you understand casting properly (if you are going to use C#) so that you can take return values and get them to be objects that have the variables you are looking to access.

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 superventure · May 19, 2012 at 01:50 AM 0
Share

Could you give an example please? I looked through here, but i'm confused as to do 'GetComponent.(a String Name)'

avatar image shadowriffe · May 19, 2012 at 01:56 AM 0
Share

You have to use something like: GameObject.Find("ObjectName").GetComponent(ComponentType)

Another option would be to add a $$anonymous$$ission tag to your mission Object. You will need to do some kind of Find before you can do a GetComponent.

avatar image
1

Answer by save · May 19, 2012 at 01:42 AM

I'm not sure I understand the string solution, is this to compare what variables are named? It's a good question but I don't see a direct solution programmatically for that. An enumeration might be of delicate use instead!

 //Player
 enum MISSIONTYPE {
     Driving,
     Hanggliding,
     DrinkBeer,
     Cooking
 }

 var mission : MISSIONTYPE = MISSIONTYPE.Driving;
 static var playerMission : MISSIONTYPE;

 function Start () {
     switch (mission) {
         case MISSIONTYPE.Driving: /*Set vars for driving*/ break;
         case MISSIONTYPE.Hanggliding: /*Set vars for hanggliding*/ break;
         default: MISSIONTYPE.DrinkBeer: break;
     }
     playerMission = mission;
 }

 //Get the enum
 function WhatMissionIsThis () {
     var mission : MISSIONTYPE = PlayerScript.playerMission;
 }


You could also make a class to better organize values between missions

 class MissionClass {
     var name : String;
     var type : MISSIONTYPE;
     //and so on
 }
 var missions : MissionClass[];
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

7 People are following this question.

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

Related Questions

Can I use strings with static variables? 2 Answers

Inputfield set as Variable 1 Answer

How to get variable from another unknown script ? 1 Answer

GetComponent with variable script possible? 1 Answer

Passing a Script Name to a Function 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