Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tarj · Jul 14, 2012 at 03:28 PM · booleanrepeat

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; } }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 14, 2012 at 04:08 PM 0
Share

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.

5 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
 }
                 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Avaista · Jul 14, 2012 at 05:38 PM 0
Share

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

avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jul 15, 2012 at 03:27 PM 0
Share

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?

avatar image Avaista · Jul 15, 2012 at 04:17 PM 0
Share

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.

avatar image
0

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.)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
  }
  
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animations in booleans loop? 1 Answer

My animation keeps on repeating on my animator 1 Answer

( Schrödinger's boolean?) custom Editor class OnSceneGUI() and OnInspectorGUI() accessing variable problem!! 2 Answers

Loading screen - Wait for script bool to be true before showing the scene? 1 Answer

Game randomly subtracts 2 lives when it is only supposed to subtract 1 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges