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 MidnightPride · Mar 11, 2012 at 11:29 AM · distanceby

Activating script by distance problem

I was working on activating a script when the player came x close to the object but, i've encountered a slight problem =/ This is the code im using

     function Update () {
 var other = gameObject.GetComponent("Script1");
 
 if ( Vector3.Distance(target.position, transform.position ) < 25) {
 other.enabled = true;
 }
 if ( Vector3.Distance(target.position, transform.position ) > 25) {
 other.enabled = false;
 }
 }

It does work however the problem is I need to activate multiple scripts rather then one so i try'd do this...

 var target : Transform;
 
 function Update () {
 var other = gameObject.GetComponent("Script1", "Script2", "Script3");
 
 if ( Vector3.Distance(target.position, transform.position ) < 25) {
 other.enabled = true;
 }
 if ( Vector3.Distance(target.position, transform.position ) > 25) {
 other.enabled = false;
 }
 }

And it didnt work I also try'd to use multiple var other = gameobject... etc and, that also didnt work I'm currently stuck =/ I'm trying to avoid having to make a script per script needing activated ( 3 scripts need to be activated so i would need 3 of these to activate all 3 )

I would appreciate some help recently i've been trying to work out problems without any help but, on this one im stuck =/

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by armoredpokey · Mar 11, 2012 at 01:00 PM

I mean, a very simple answer would be

 function Update () {
   var other1 = gameObject.GetComponent("Script1");
   var other2 = gameObject.GetComponent("Script2");
   var other3 = gameObject.GetComponent("Script3");
   if ( Vector3.Distance(target.position, transform.position) < 25 ) {
     other1.enabled = true;
     other2.enabled = true;
     other3.enabled = true;
   } else {
     other1.enabled = false;
     other2.enabled = false;
     other3.enabled = false:
   }
 }

Not the prettiest solution, but you certainly don't have to make three different scripts to do what you want.

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 MidnightPride · Mar 11, 2012 at 10:46 PM 0
Share

I try'd that before ( and i try'd yours ) and, it doesn't work =/ ( I replaced the Script1 with my scripts )

avatar image
0

Answer by TheLedworks · Mar 11, 2012 at 01:00 PM

You need to use multiple variables, just do the same for...

var script1 : Script = gameObject.GetComponent("Script1");

var script2 : Script = gameObject.GetComponent("Script2");

var script3 : Script = gameObject.GetComponent("Script3");

instead of trying to store three references in one var. Ha ha, how does the expression go? don't put all your eggs in 1 basket :D

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 Jacek_Wesolowski · Mar 11, 2012 at 12:56 PM

It looks like you need to call GetComponent() three times, because it's not smart enough to take multiple arguments. It only accepts one at a time.

You need to store results in three separate variables, eg. other1, other2, other3. If you assign the returned value to other every time, it will only keep the last value you assigned to it, and not all three. On top of that, you can't declare the same variable more than once (that's what the var keyword does - it declares variables).

Also, in the interest of better performance, you most likely don't need to call GetComponent() on every Update(). Instead, turn other1, other2 and other3 into class fields (rather than local variables) and fill them with values in Start(). The values will be remembered for as long as your object exists, whereas local variables are lost when the program returns from function.

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

7 People are following this question.

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

Related Questions

Terrain shadow distance and lighting problem 0 Answers

Getting a custom class to contain a certain script. 1 Answer

Problem with variables 1 Answer

Help whit Animations play? 0 Answers

AI distance from Player 3 Answers


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