Writing a bool to a separate script without scripts name?

Hello.

At the moment, I am currently trying to change a bool to true from simply finding the monobehaviour that the bool is located in. I am able to find the component, (each monobehaviour has a separate name, but has the same bool, that I need to change with a ray), but the engine does not recognize the bool. It gives me this error: "Type Component does not contain a definition for [bool name] and no extension method [name of the bool] of type 'Component' could be found. Are you missing using a directive or an assembly reference?"

The problem is that, until I can show it, in the game, what object the script is in, I am unable to point it to it, thus, it can’t find the name of the bool, and I can’t even run the game.

Is there any way of having it let me bypass that, by checking first in the code some how if it has the bool within the monobehaviour, so then it will understand that I will not have the script with the bool until it is time?

(Basically, I hover the mouse over an object, to get the code. And each object has a different code, so I can’t specify it by name, but need to find the bool with code.)

Thank you in advance for any assistance or help! I am sorry. Thanks for reading!

Given multiple scripts each with the same boolean, see @asafsitner’s answer to this question:

http://answers.unity3d.com/questions/381529/how-to-address-scripts-without-knowing-their-names.html

I’ve referenced this question a number of times for various questions. Even though he states that overriding a base class is, “which is arguably worse practice than the previous options,” it is the solution which most seem to use.

You have a couple of options. The first is to implement a common interface on all of your components. This will let you access the component as the type of the interface instead of needing to know its exact type.

The second is to use reflection. This lets you access members without knowing any of the details in advance. In most cases reflection can be avoided with a decent structure.

Alright. I’ve managed to solve the issue! Thank you to you two that answered. And for that link! That helped me in working towards solving the issue ^-^

I solved the issue, by simply using Component.SendMessage()

EDIT: This is C#.

I’ll give an example, in case someone from the future stumbles upon my question:

//In the script that contains the code that >finds< the MonoBehaviour component that we do not yet know the name of, can be used with the SendMessage function like so:


private GameObject exampleObject;

void ExemplaryUpdate()
{
      exampleObject = (code that finds the game object that contains the component)
      
      exampleObject.GetComponent<MonoBehaviour>().SendMessage("NamelessCodeFunction", true);
}

//In the "nameless" script that we are sending a value to:


private bool namelessScriptBool = false;


void NamelessCodeFunction(bool sakuriBool)
{
      namelessScriptBool = sakuriBool;
}

I didn’t test this after writing it, of course.
Though, I hope I could help someone by finding a solution! Thanks again for the help you two ^-^

Have a good day, everyone.