Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Crazy4FUN · Sep 11, 2017 at 08:16 PM · variablegetcomponentstring

How to get variable from another unknown script ?

Hi, I'm beginner on Unity and I didn't find an answer to my question on the web, so I ask it here (and sorry if my English is not perfect, I'm French). So, I created a quest system that use many scripts with the same variables names, but with different names which allows me to identify them (Example : quest_1_1_201, the first number is the ID of the quest, the second is the number of steps to finish it.. but the script variables names are the same than quest_2_1_101 for example), and I want to get their variables without knowing their names, just with a string that display the current quest. For example, if I want to know if the currentStep of the quest is 3, I tried this :

 public string currentQuestID;
 
 void Update () {
     currentQuestID = GetComponent<RPGQuestManager>().currentQuestID;

     //in the quest manager script are listed all the quest that are accepted by the player, but only one can be selected

     if (GetComponent(currentQuestID)) {
         if (GetComponent(currentQuestID).currentStep == 3) {
             Debug.Log ("It works !");
         }
     }
 }

But it doesn't work, I don't know how to access this variable, I spent hours on this forum and I tried lot of methods but none has worked, or it was just to hard for me.

Anyone could help me please ?

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 Positive7 · Sep 11, 2017 at 08:33 PM 1
Share
 void Update()
     {
         currentQuestID = GetComponent<RPGQuest$$anonymous$$anager>().currentQuestID;
 
         if (currentQuestID != string.Empty)
         {
             if (GetComponent<RPGQuest$$anonymous$$anager>().currentStep == 3)
             {
                 Debug.Log("It works !");
             }
         }
     }
avatar image Crazy4FUN Positive7 · Sep 11, 2017 at 08:53 PM 0
Share

Thanks for your answer, but the quest manager isn't important, the variable currentStep is on the script named with a string (currentQuestID), that can be "quest1", "quest2".. but not a specific name like "RPGQuest$$anonymous$$anager", I want to get a variable with the string name of the script.

1 Reply

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

Answer by pslattery · Sep 11, 2017 at 09:39 PM

I think you have the right idea, and the wrong implementation. The way I'd suggest structuring would be to have your RPGQuestManager contain an array of Quests, which contains references to all quest scripts.

This means each quests should implement an interface that provides functions to access the data you need like getting the current step. Coding to an interface is explained well here.

On Awake, each quest should register itself with the RPGQuestManager. This means that RPGQuestManager should have a RegisterQuest function which takes a Quest as an argument and places it in the array of Quests.

Finally, RPGQuestManager should have a variable called CurrentQuestIndex which is just an int and which represents the index of the current quest. You should then be able to access the current quest via that index.

Hope this helps!

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 Crazy4FUN · Sep 12, 2017 at 11:00 AM 1
Share

Hmm.. Your answer made me think about the way to access the quest scripts ! I have already an array with the quest ID, names and objective, and I thought that it was too long and complicated to set all values for all quest in the RPGQuest$$anonymous$$anager, but I just have to set it for the current quest. I just have to use the quest scripts and they know themselves if they are the current quest, and if yes they set there values in the RPGQuest$$anonymous$$anager. It was so simple !

This is not exactly what you proposed, but thanks you very much for your answer !

avatar image pslattery Crazy4FUN · Sep 13, 2017 at 04:56 PM 0
Share

No worries! You know more about how you've structured your game than I do. Glad I could help.

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

74 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponent from string name? 3 Answers

Passing a Script Name to a Function 2 Answers

GetComponent with variable script possible? 1 Answer

GetComponent() - Is it possible to pass a string variable as name of the script? 3 Answers

Variable from a external script not being effected 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