Set bool to different objects with 1 script

Hello i readed all google pages and everything i could find but nothing helped me so i have to ask here: i made a script attached to 100 cubes it says

using UnityEngine;
using System.Collections;

public class IfClose : MonoBehaviour {
	public bool Gravitronned= false;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnTriggerEnter (Collider col)
	{
		if (col.gameObject.name == "IfInScreenGrv") {
			Gravitronned = true;

		}
		else 
		{
		Gravitronned = false;
		}
	}
}

and then i have another script that says

using UnityEngine;
using System.Collections;

public class TimeFreeze : MonoBehaviour {
	static bool GravitronedTwice;

	// Use this for initialization
	void Start () {
	
	}
	
	void Update ()
	{
		GravitronedTwice = gameObject.GetComponent<IfClose> ().Gravitronned;
                      if (GravitronedTwice = true) {
			        if (Input.GetKeyDown (KeyCode.V)) {
				        Physics.gravity = new Vector3 (0, 3.0F, 0);


			         }
		       }
}
}

so when i press V i want the cube only in this area to get
Physics.gravity = new Vector3 (0, 3.0F, 0);

others stay ass they are

To properly format your code use the 101 010 button. For more information on how to effectively use Unity Answers watch the Unity Answers tutorial https://www.youtube.com/watch?v=ezAPpViLs2Q and read the faq: http://answers.unity3d.com/page/faq.html.

1 Answer

1

Change:

if (GravitronedTwice = true) {

to

if (GravitronedTwice == true) {

They still go up togather

Get rid of the static keyword, you dont want that. That makes that variable shared across every instance.