x


How to repeat with Boolean?

Hi there! I need to know how i can repeat this Script with a Boolean. So there's a Speeder and when it comes near the Location point, it'll be activated ONCE, and then it ends. So how can i reacivate this Script when i come near the same Point a Second Time?

I receive this: Expressions in statements must only be executed for their side-effects. But i don't know why.

var speeder : GameObject;
var shadow : GameObject;
var TestLoc : GameObject;
private var aggroRadius :float = 150;
var isActive : boolean = true;

function Start ()
{
 speeder = GameObject.Find("DummySpeeder");
}

function Update ()
{
 if (isActive)
 {

var distance = Vector3.Distance(speeder.transform.position, transform.position); if distance < aggroRadius) {

Instantiate (TestLoc,transform.position, transform.rotation); Instantiate (shadow,transform.position, transform.rotation);

Destroy(gameObject); } }

else { distance > aggroRadius; } }

more ▼

asked Jul 14 '12 at 03:28 PM

Tarj gravatar image

Tarj
1 1 1

The code kills itself (Destroy(gameObject);) so you can't activate it twice.

Your last para: the final else { distance&gt;aggroRadius; } isn't anything. If you don't want to do anything when isActive is false, then don't do anything -- don't have an else.

Jul 14 '12 at 04:08 PM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

Okay, so it doesn't repeat the Script and only one Locator is working, but ignoring the Aggro Radius. When i press Play it spawns, whereever i am.

function Update()
{
     if(isActive)
     {
         var distance = Vector3.Distance(speeder.transform.position, transform.position);
         if (distance < aggroRadius) 
         {
              Instantiate (TestLoc,transform.position, transform.rotation); 
              Instantiate (shadow,transform.position, transform.rotation);

              isActive = false;
              StartCoroutine(ExitTestLoc());
         }

     }
}

function ExitTestLoc()

{
    yield;
        while(Vector3.Distance(speeder.transform.position, transform.position) > aggroRadius);
        yield;
    isActive = true;
}
more ▼

answered Jul 16 '12 at 01:12 PM

Tarj gravatar image

Tarj
1 1 1

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

So it does not Run anyway. Nothing spawns, nowhere. So isn't there a Possibility to do this Loop with a simple Boolean true/false thing?

Something like this? So this does'nt repeat it if i leave the Place... but maybe you can help me?

function Start ()
{
 speeder = GameObject.Find("DummySpeeder");
}

function Update ()
{
 if(isActive)
 {
 var distance = Vector3.Distance(speeder.transform.position, transform.position);
 if (distance < aggroRadius)
 {
 Instantiate (TestLoc,transform.position, transform.rotation);
 Instantiate (shadow,transform.position, transform.rotation); 

 Destroy(gameObject); 
 }
 }
 if (distance > aggroRadius)
 {
 isActive = true;
 }

}
more ▼

answered Jul 16 '12 at 01:12 PM

Tarj gravatar image

Tarj
1 1 1

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

Firstly, if your looking for distance of the speeder, why not use a Sphere Collider?

Then you could just use OnTriggerEnter and OnTriggerExit

however, if you must do it this way from some reason

public void Update()
{
     if(isActive)
     {
         float distance = Vector3.Distance(speeder.transform.position, transform.position); 
         if distance < aggroRadius) 
         {
              Instantiate (TestLoc,transform.position, transform.rotation); 
              Instantiate (shadow,transform.position, transform.rotation);
              isActive = false;
              StartCoroutine(ExitWhatShouldHaveBeenATriggerCollider());
         }
     }
}

IEnumerator ExitWhatShouldHaveBeenATriggerCollider()
{
    yield return null;
    while(Vector3.Distance(speeder.transform.position, transform.position) < aggroRadius)
        yield return null;
    isActive = true;
}
more ▼

answered Jul 14 '12 at 05:36 PM

Avaista gravatar image

Avaista
484 8 23 31

Also, like what was said above, DestroyObject... this kills the gameObject.

Jul 14 '12 at 05:38 PM Avaista
(comments are locked)
10|3000 characters needed characters left

Thanks a lot! i've now this:

var speeder : GameObject;
var shadow : GameObject;
var TestLoc : GameObject;
private var aggroRadius :float = 150;
var isActive : boolean = true;


function Update()
{
     if(isActive)
     {
         var distance = Vector3.Distance(speeder.transform.position, transform.position);
         if (distance < aggroRadius) 
         {
              Instantiate (TestLoc,transform.position, transform.rotation); 
              Instantiate (shadow,transform.position, transform.rotation);
              isActive = false;
              StartCoroutine(ExitTestLoc());
         }
     }
}

IEnumerator ExitTestLoc()

{
    yield return 0;
    while(Vector3.Distance(speeder.transform.position, transform.position) > aggroRadius);
        yield return 0;
        isActive = true;
}

Now i receive an Error: UCE0001: ';' expected. Insert a semicolon at the end. But i can't see any missing Semicolon there.... could that be a Bug?

more ▼

answered Jul 15 '12 at 01:37 PM

Tarj gravatar image

Tarj
1 1 1

Your original code is uscript, and Aviasta's code is C#. For example, if you look up yield in the Unity online docs, uscript and C# are different.

I think it might help if you think more about what is supposed to happen in the game. If the code above worked, you could stand just at the edge of the area, moving a tiny bit forward and back to keep spawning shadows. Is that what you want?

Jul 15 '12 at 03:27 PM Owen Reynolds

You should replace

 yield return 0;

with

 yield;

I think, I do not use JavaScript.

Also, after your while loop in the coroutine function, you have a semicolon. In C# this would result in an infinite loop, and I am 99% sure it will in JS as well, once the condition is true.

If you get it working, like owen said, test it out and see if it has the behavior you want.

Jul 15 '12 at 04:17 PM Avaista
(comments are locked)
10|3000 characters needed characters left

What i need to do is only a repeat when i reach the point one more Time. So they spawn on a track. 6 Locators are on it, and it must be, that they spawn again and again, whenever i pass the Point. (i've used Javascript. Don't know another.)

more ▼

answered Jul 15 '12 at 08:31 PM

Tarj gravatar image

Tarj
1 1 1

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

x240
x42

asked: Jul 14 '12 at 03:28 PM

Seen: 316 times

Last Updated: Jul 16 '12 at 01:12 PM