GetComponent with variable script possible?


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 :smiley:



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:


	public string myName = ("Character");
	public bool isSelected = false;
	public string GetMyName()
		return myName;

	void Awake () 
	void Update () 
			SendMessage("setStatus", true);	

And here is the receiving end in the ChatLog_ itself:

void setStatus(bool is_Selected)
		SelectionController selector;
		selector = gameObject.GetComponent<SelectionController>();
		isSelected = is_Selected;
		selector.isSelected = false;
		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;

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));