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 CGPHANT0M · Feb 26, 2013 at 04:30 AM · c#variablegetcomponentstring

GetComponent with variable script possible?

EDIT

OK, I'm a fool. I completely looked too deep into this. Instead of bouncing messages around my scene, or trying to use a variable with the get component.

But, here's a fun thought.. Why not have the variable scripts check the constant script for change? And not have the one constant script try to check all the variables?

I've edited this so people know I've solved the issue here, but It would still be cool to see if there is a way to assign a variable to this function.

Mods, feel free to delete if you don't want to indulge my curiosity :D

/EDIT

Hello.

I'm currently writing a dialogue system.

I have 4 main components.

A "SelectionController" script which is assigned to each "character" that has dialogue. This script is pretty simple and just there to recieve information from a "PlayerStatus" script, which just handles the mouse input (raycast) and updates the Player objective. When the player selects a character, the playerstatus script changes the "isSlected" variable of the gameobject selected's slectioncontroller to true. The selesction controller will then load a ChatLog Scipt, which is unique for each character except for it's basic function, where it sends string data via an array to a chat controller script, which just updates everything to the screen.

The issue I am having is loading the correct script for a particular character. As the scripts have different names (is there a way around that?).

I used this to call the script originally as the selection manager script is assigned to each character.

 ChatLog_Villager chatLog;
 chatLog = gameObject.GetComponent<ChatLog_Villager>();
 chatLog.isSlected = true;

And this works fine for a single character. Now, I have the slectioncontoller returning variable character name data, but I can seem to find online any information about using variable script. It would be ideal to use a variable to feed in the getcomponent information, especially in making it public so I can drag and drop the correct ChatLog_ in the inspector.

As I couldn't figure this out I resorted to using sendmessage();

And this works initially too, here's the code:

SelectionController:

     public string myName = ("Character");
     public bool isSelected = false;
     
     public string GetMyName()
     {
         return myName;
     }
 
     void Awake () 
     {
     }
     
     void Update () 
     {
         if(isSelected)
         {
             SendMessage("setStatus", true);    
         }
     }
 }

And here is the receiving end in the ChatLog_ itself:

 void setStatus(bool is_Selected)
     {
         SelectionController selector;
         selector = gameObject.GetComponent<SelectionController>();
         
         if(selector.isSelected)
         {
         isSelected = is_Selected;
         selector.isSelected = false;
         }
         else
         {
         is_Selected = false;
         }
     }

And here I change the isSlected varible of the selectionController to false, so it wont call sendmessage(); every frame.

Anyway, this seems to work just fine for one character, but for once you add another character to the scene it doesn't work. only one of them can be selected. The other just wont seem to be selected, in the inspector you can see the variable checbox, but it remains UN-checked, whereas the first one becomes checked (as expected).They both have the slectioncontroller script assigned to them. But they have their own chatLog_, (e.g ChatLog_Villager and ChatLog_Soldier) which contains exactly the same code except the dialog is different.

So, I have been looking around trying to find out if it was possible to use a variable get component. It would be great it I could just feed it a string variable that can be set in the inspector.

Something like:

 Public dtring chatLogName;
 
 
 ChatLogName chatLog; //<<<not sure how this would work
     chatLog = gameObject.GetComponent<ChatLogName>();
     chatLog.isSlected = true;
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

1 Reply

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

Answer by Tarlius · Feb 26, 2013 at 05:15 AM

I'm not sure I've understood your question correctly, but I think you could more easily solve your problem using inheritance.

Here is a cut-back example of how to implement with both a base class and an interface

 public abstract class SelectableObjectBase : MonoBehaviour{
     public abstract void OnSelect();
 }
 // With interface:
 //public interface ISelectableObject {
 //    void OnSelect();
 //}
 
 public class ChatLog : SelectableObjectBase
 //or: public class ChatLog : MonoBehaviour , ISelectableObject {
     public override void OnSelected() {
         // Your logic goes here
     }
 }
 
 public class UIManager : MonoBehaviour {
     void Update) {
         GameObject go;
         // Some logic to get go, maybe a raycast or whatever
         SelectableObjectBase so = go.GetComponent<SelectableObjectBase>();
         // or: ISelectableObject so = go.GetComponent(typeof(ISelectableObject));
         so.OnSelected();
     }
 }
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 CGPHANT0M · Feb 26, 2013 at 12:18 PM 0
Share

I will mark this as the right answer, as after some reading, and experimenting with a similar setup for AI, this seems like the best solutions, and would certainly cure all of the ales current present in my mechanic. That said, my dialog setup is now working, and I'm no longer using the Send$$anonymous$$essage() function, which apparently is good to avoid. Anyway, thanks for the great advice Tarlius, as I mentioned I did some reading, and followed a tutorial setting up AI in this manner. Still a bit over my head, but certainly the right direction to head should anyone else be wondering the same.

avatar image Tarlius · Feb 26, 2013 at 12:28 PM 0
Share

No problem, glad I could help. Send$$anonymous$$essage can be convenient sometimes, but I've had to fix so many bugs caused by people typoing the message that now I throw things at people in the office that do it.

Ok, I don't, but I really want to >.>;;

Anyway, stick with it and it'll be second nature soon enough! ^^b

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

10 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

Related Questions

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

Multi similar script Components same game object 1 Answer

Passing a Script Name to a Function 2 Answers

Enemy variable remains null 1 Answer

Can i give GetComponent a variabel instead of a scriptname? 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