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 Chipmunk9998 · Dec 21, 2011 at 06:20 AM · javascriptgameobjectvariable

Getting variable from another script

Hey, I'm having trouble getting a variable from another script. This is my code:

 private var network : GameObject;
 private var network1;
 
 network = GameObject.Find("NetworkController");
 network1 = network.GetComponent(ChatController);
 
 function Update () {
 
 if (network1.typingMessage) {
 
     GetComponent(CharacterMotor).enabled = false;
     GetComponent(CameraSwitch).enabled = false;
     GetComponent(Noclip).enabled = false;
 
 }
 
 if (!network1.typingMessage) {
 
     GetComponent(CameraSwitch).enabled = true;
     
             if (GetComponent(Noclip).on) {
             GetComponent(Noclip).enabled = true;
             }
         
             if (!GetComponent(Noclip).on) {
             GetComponent(CharacterMotor).enabled = true;
             }
 
 }
 
 }

What it does is it gets a GameObject called "NetworkController", and then gets the script called "ChatController" attached to it.

Then in the Update function it checks to see if a variable from the "ChatController" script called "typingMessage" is true, and if it is, it disables 3 scripts.

Then in the next if statement, it checks to see if the "typingMessage" variable is false, and if it is, it enables the "CameraSwitch" script. Then it only enables the Noclip script if the "on" variable from the Noclip script is true, and it enables the CharacterMotor script if the "on" variable is false.

The CharacterMotor, CameraSwitch, Noclip, and this script are all on the same object.

If anyone could tell me what I'm doing wrong, that would be great...

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by syclamoth · Dec 21, 2011 at 06:49 AM

While you haven't told us exactly what error messages you are getting, there are a few things you could do straight off to improve the code here.

1: Move all the stuff that happens 'outside of any functions' into an explicit 'Start' function. It looks nicer.

2: Staticly type network1- make sure that it knows what it is supposed to be from the moment it is declared.

 private var network1 : ChatController;

3: Instead of using GameObject.Find(string), just make network public and assign it in the editor! This way you don't have to worry about spelling errors in your hardcoded script.

4: You are calling GetComponent up to 6 times every frame here. GetComponent is quite a slow function, and it would be better just to do it once and remember the results. In your new 'Start' function, get all the values you would retrieve in that way, and then substitute that into all the places you would have used them in Update.

 var network : GameObject;
 private var network1 : ChatController;
 private var motor : CharacterMotor;
 private var switch : CameraSwitch;
 private var noclip : Noclip;

 function Start()
 {
     network1 = network.GetComponent(ChatController);
     motor = GetComponent(CharacterMotor);
     switch = GetComponent(CameraSwitch);
     noclip = GetComponent(Noclip);
 }

5: You seem to be checking every frame for if the chatController is typing, and then resetting every value every frame, even if it hasn't changed. There are a few fixes for this- first, you could keep a boolean which it first checks the current value of network1.typingMessage against, and if they are not the same it executes the rest of the function. You could change it all to a callback model, where the chatController has a reference to this script, and tells it whenever it starts or stops typing.

If you do all of that, see if your problems persist. Otherwise, post a comment with more details about exactly what is going wrong with your script!

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 Chipmunk9998 · Dec 21, 2011 at 09:38 AM

I found out why it wasn't working, I forgot to include two quotation marks here:

 network1 = network.GetComponent(ChatController);


It now looks like this:

 network1 = network.GetComponent("ChatController");


But thanks for your help, I'm kind of new to scripting and your advice really helped me clean up my code.

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 Lo0NuhtiK · Dec 21, 2011 at 10:04 AM 1
Share

??-> "It is better to use GetComponent with a Type ins$$anonymous$$d of a string for performance reasons."
Edit : I'm guessing you wouldn't need to use the quotes/string-type if your variable declaration of network1 was done the way @syclamoth said ... var network1 : ChatController , and then wake it up/ start it up to assign it rather than it being out in nothingness.

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

Script that stores gameobject hit by raycast keeps getting a NullReferenceException error. 2 Answers

Scripts accessing one another (JS) 0 Answers

Assigning current color to a variable for fade out (C#) 0 Answers

Game Object referencing from another script 0 Answers

Communication between objects and other scripts, variables and properties 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