x


Changing Booleans Throught Certain Distance

I am trying to change a boolean on a single or multiple objects through a certain radius.

I was thinking of having these variables set up

public int Radius = 1;
public GameObject Activate = GameObject.FindWithTag("activated");

I was thinking that you guys at unityanswers could help me make a script that would find GameObjects with the tag activated and if there in a certain distance the script placed on those objects would have a boolean and it would make it true.

more ▼

asked Sep 20 '11 at 12:29 AM

GameNewb gravatar image

GameNewb
1 2 2 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

how about this-

public double radius = 1;

private Transform myTrans;

Start()
{
    // This makes it so that you don't need to do as many inefficent
    // dynamic searches, assuming you want to do this every frame
    myTrans = transform;
}


// Then, wherever you need to do the check-

GameObject[] foundObjects = GameObject.FindGameObjectsWithTag("activated");

foreach(GameObject obj in foundObjects)
{
    ScriptWithBooleanOnIt objScript = obj.GetComponent<ScriptWithBooleanOnIt>();
    if(objScript == null)
    {
        // This makes sure that your object in question contains the script
        continue;
    }
    // This will also set the value to false if the object goes back out of range.
    objScript.booleanValue = Vector3.Distance(obj.transform.position, myTrans.position) < radius;
}

And then put a MonoBehaviour on every object you want to affect with this called 'ScriptWithBooleanOnIt' (name not important), and make sure it has a public bool called 'booleanValue'.

more ▼

answered Sep 20 '11 at 02:03 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x794
x321
x239
x137
x49

asked: Sep 20 '11 at 12:29 AM

Seen: 509 times

Last Updated: Sep 20 '11 at 02:03 AM