- Home /
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; } }
The code kills itself (`Destroy(gameObject);`) so you can't activate it twice.
Your last para: the final else { distance>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.
Answer by Avaista · Jul 14, 2012 at 05:36 PM
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;
}
Also, like what was said above, DestroyObject... this kills the gameObject.
Answer by Tarj · Jul 15, 2012 at 01:37 PM
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?
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?
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.
Answer by Tarj · Jul 15, 2012 at 08:31 PM
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.)
Answer by Tarj · Jul 16, 2012 at 01:12 PM
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;
}
Answer by Tarj · Jul 16, 2012 at 01:12 PM
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;
}
}