Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 mattt4001 · Jul 14, 2011 at 10:29 PM · variableslocalglobalwaypoints

Editing local variables in another script

I am working on a waypoint script and am having trouble with global variables.

It works by triggers. When the waypoints detect a collision from an AI, they advance a counter on the AI in order for it to go to its next waypoint. What is does now is add to the counter on a Global scale, not local. This causes all of the AI to move even if only one is triggered. How can I alter the variable on only the local level?

The following code is on each individual waypoint.


function OnTriggerEnter(other : Collider)
{
   MoveNext(other);
}
function MoveNext(other : Collider)
{
    yield new WaitForSeconds (10);
    other.GetComponent(AI).IncreaseWayPoint();
 }

This next code is on the AI

private var currentWaypoint : int; function IncreaseWayPoint () { currentWaypoint++;

 }

Also, a little off topic, why doesn't the yield command work here?

Thanks!

Comment
Add comment · Show 2
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 Eric5h5 · Jul 15, 2011 at 05:21 PM 0
Share

OnTriggerEnter itself can be a coroutine; you don't need to launch another coroutine from it.

avatar image mattt4001 · Jul 16, 2011 at 12:41 AM 0
Share

Thanks for the responses. I figured out my problems, they were located in a different part of the script. Also your explanations are very useful.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DaveA · Jul 14, 2011 at 10:43 PM

You don't need 'new' in yield

Odd, that looks like it would work. It's not a static var is it?

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 mattt4001 · Jul 15, 2011 at 04:15 PM 0
Share

No it is a private var. I still have no idea why it is doing this.

And removing the 'new' doesn't seem to help.

avatar image
0

Answer by aldonaletto · Jul 16, 2011 at 12:31 AM

There's a problem with that yield: it will start a 10s pause, then change the waypoint - but if the enemy enters the trigger again before the pause ends, another independent cycle will be started, and 10s after the waypoint will be incremented again. If the enemy enters and exits the trigger several times in a few seconds, several cycles will be started, with the enemy changing waypoints 10s after each OnTriggerEnter event.
Maybe I'm wrong, but I suppose your idea is to create a 10s dead time during which no more waypoint increments are allowed. If this is true, you should change the logic. The trigger enter should be:

 function OnTriggerEnter(other : Collider)
 {
     other.GetComponent(AI).IncreaseWayPoint();
 }

And the AI script should do the following:

 var busy: boolean = false;
 
 function IncreaseWayPoint (){
     if (!busy){ // does nothing if there's an increment cycle going on
         busy = true; // signal a cycle has started
         yield WaitForSeconds(10); // starts pause
         currentWaypoint++; // 10s after inc waypoint
         busy = false; // cycle ended
     }
 }

I hope this does what you want to do; if not, please let me know.
NOTE: If you really want a dead time, it would be better to increment the waypoint before the pause.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing variables from seperate scripts 1 Answer

Global or local variables for use at start function just once 2 Answers

Local Variables 2 Answers

Transform global quaternion from local quaternion 2 Answers

Not asigned health script 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