Change variable in c# script A from c# script B???

I have two scripts, PlayerController.cs and DestroyByContact.cs. As one of the things to do inside of an If statement, I want the DestroyByContact script to access the PlayerController script, and change it’s boolean variable “canControl” to false. If it helps, the Player Controller script is attached to the “Player” object, and the DestroyByContact script is attached to the “Asteroid” object. I’m really new to this scripting stuff, and I’ve been slaving over this for days, but I can’t get it right. Please be as exhaustive and long-winded as you need in order to explain this simply. I need to know the steps to take, in order, what code to put where in the script, and all that jazz. Any help is immensely appreciated!!!

You can create static variables

public static bool canControl

In DestroyByContact you can write this:

if (PlayerController.canControl) {
 // do something
 }

Both of them must be C# or JS.

This is not effective way. If you further will keep doing like this with all other scripts, it will create code that is very hard to read.

If you’d like to set a variable in one c-sharp script attached to a GameObject, from another c-sharp script attached to another GameObject, you can do so inside the inherited function/method OnTriggerEnter. Inside DestroyByContact.cs, add the following to your class:

	public void OnTriggerEnter(Collider collider)
	{
		if (collider.gameObject.name == "Player")
		{
			Player player = collider.GetComponent<Player>();
			player.canControl = false;
		}
	}

Inside your Player script, make sure your canControl variable is public, so that other scripts can access it externally. It should look something like this:

public bool canControl;

Additionally, you’ll need to set up some things in your scene. Firstly, create two spheres (or anything with some geometry so you can see what’s happening in your scene, use GameObject > Create Other > Sphere). These spheres should come equip with sphere colliders. In each sphere collider’s inspector, click the ‘Is Trigger’ check box to enable it. The collider will now detect collisions but won’t physically react to them, it will just fire a method in our code when one such collision occurs.

Next, add to each sphere a Rigidbody (Component > Physics > Rigidbody). The rigidbodies are required for the spheres to move around and still detect collisions. You might want to click each check box under ‘Constraints’ so that the Rigidbody doesn’t move around by itself.

Finally, attach (drag and drop onto your GameObject in the hierarchy) your DestroyByContact.cs script to the sphere you wish to represent your asteroid, the other of which will be your player. Rename the other sphere, the one that is your player, to ‘Player’ (excluding the quotation marks).

That’s it! When Unity detects a collision between your asteroid and any GameObject, it will call your OnTriggerEnter method with information about what your asteroid collided with. Your script will then check if the name of what it collided with is indeed ‘Player’, and if it is, It will get the Player script attached to it’s GameObject and then set the public variable ‘canControl’ to false.