Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by straydogstrut · Apr 05, 2010 at 01:02 PM · variablegetcomponentscriptingbasics

Accessing variables in another script using GetComponent()

I've just run across a showstopper when I built a webplayer version of my game, oh dear=S At a certain point in my game I initiate a timer on a script attached to my player character that, when it runs out, sets a variable on a script attached to an npc to true, which causes it to move along it's waypoints etc.

It works fine in the editor, hence why I didn't see any problem previously, but it doesn't work in a webplayer or a standalone app. I tested using one of my guiText functions to show a message after the variable has been set - I never see this message outside the Editor so it looks like the variable is not being set. If I set the isMoving variable to true initially, the npc moves as expected, so the problem must lie with the way I am accessing the variable on the other script.

My code at the moment is below (note I have commented out the line that finds the npc gameobject since I have moved this into the Start() function:

if(sinkTimer > cleanerResponseTime){

 // cleaner = GameObject.Find("cleaner");
 otherscript = cleaner.GetComponent("wp_scriptNew1"); 
 otherscript.isMoving = true;

 if(otherscript.isMoving){
     TextHints.message = "isMoving is true";
     TextHints.textOn = true;
 }

}

I should point out that multiple npcs have this script attached so I cannot use static variables. I tried to use the method described in the docs here for exposing references to other objects, but I get a warning that a local variable called otherscript already exists. That's true since the code that initiates the timer stores its reference to another script in a variable called otherscript too. However, I have not declared these variables at the top of my script so I assumed they could be treated as temporary variables? (like when using 'i' in a for loop for example).

Renaming one of them and using the code from the docs allows it to work, so:

var otherscript2 : wp_scriptNew1;

function Update(){

if(sinkTimer > cleanerResponseTime){

     otherscript2.isMoving = true;

     if(otherscript2.isMoving){
         TextHints.message = "isMoving is true";
         TextHints.textOn = true;
     }
 }

}

But I still don't understand why using GetComponent didn't work, since the other piece of code which still uses it successfully sets a boolean to start the timer above in the first place.

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 DAQ · Sep 14, 2011 at 03:02 AM 0
Share

Without seeing your whole script I can only speculate that you had a globally scoped variable called otherscript of a specific type other than wp_scriptNew1.

0 Replies

· Add your reply
  • Sort: 

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

1 Person is following this question.

avatar image

Related Questions

updating a gameobject variable from another script attached to another object 3 Answers

Changing Prefab Variables From Another Script In Editor 1 Answer

Want to add ship's velocity to Instantiated projectile 2 Answers

Problem with 2 scripts communicating 0 Answers

Taking value of variable from a script of Gameobject to a script attached to other Gameobject 0 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