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 PerfectOrigin · Apr 07, 2012 at 04:23 AM · guierrorconnection

script connection

I have two JavaScript files one for a main building in my rts and one for a control point. I have managed to get them to some what communicate but the GUI in my control point script gets messed up, how can I fix this? I am still relatively new to scripting so be gentle. Thanks in advance

main building script-

 //MainBuildScript
 
 
 var curMoney : float;
 var mob1 : GameObject;
 var theLocation : Transform;
 var check : int = 1;
 var conCheck : int;
 var loop : boolean = true;
 
 
 var go = GameObject.Find("controlPoint");
 
 
 function Update ()
 {
     curMoney = go.GetComponent(controlPointScript2).curRes;
     
     conCheck = go.GetComponent(controlPointScript2).checkMe;
     
     if(check > 1)
     {
         check = 1;
     }
 }
 
 
 function OnGUI()
 {
     
         
         GUI.Label(Rect (10, 10, 100, 20), "Main");
         
          if (GUI.Button(Rect(10,30,80,30), "Worker"))
          {
              if(curMoney >= 5.0)
              {
                  loop = true;
                  
                  mob1.SetActiveRecursively(true);
                  Instantiate (mob1 , theLocation.position, transform.rotation);
                  mob1.SetActiveRecursively(false);
                  
                  subtractFive();
                  
 
              }
          }
          
     
 }
 
 function subtractFive ()
 {
     if(loop) 
     {
         //Subtract Money
         check = 2;
         loop = false;
     }
     
 }




Control Point Script -

 var curRes : float = 0;
 var timer : float;
 
 var go2 = GameObject.Find("testMainBuilding");
 
 var checkMe : int;
 
 function Update ()
 {
     checkMe = go2.GetComponent(mainBuildScript).check;
     
     if(curRes > 1000)
     {
         curRes = 1000;
     }
     
     if(checkMe >= 2)
     {
           curRes = curRes - 5.0;    
     }
     if(curRes <= 0)
     {
         curRes = 0;
     }
 }
 
 function OnCollisionStay (thing : Collision)
 {
 
     
     timer += Time.deltaTime;
     
     if(thing.gameObject.tag == "control")
     {
         if(timer >= 5)
         {
               curRes = curRes + 5.0;
               timer = 0;
           }
           
     }
 
 
 }
 
 function OnGUI()
 {
         GUI.Label (Rect (110, 10, 100, 20), "Resources: " + curRes);    
 }
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

1 Reply

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

Answer by aldonaletto · Apr 07, 2012 at 04:41 AM

You can't declare a variable and assign GameObject.Find(...) to it in the same line (as well as many other Unity functions or properties). Furthermore, the code outside any function is executed at Awake, when the object you're trying to find may not exist yet.
To avoid these problems, you should just first declare the variable, then assign Find at Start():

... var go: GameObject; // declare the variable first...

function Start(){ // and find the object at Start(): go = GameObject.Find("controlPoint"); } The same applies to the second script.

Comment
Add comment · Show 5 · 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 PerfectOrigin · Apr 07, 2012 at 05:39 AM 0
Share

I did what you said but I still get the GUI messing up and for some reason, the cur$$anonymous$$oney and curRes variables are still not equal. Any further help would be greatly appreciated!

avatar image PerfectOrigin · Apr 07, 2012 at 05:43 AM 0
Share

the cur$$anonymous$$oney is not affected by the -5 for some reason?

avatar image Kleptomaniac · Apr 07, 2012 at 07:42 AM 0
Share

Yep, and that would be because the Control Point Script can't find the check variable in your mainBuildScript because it can't find the object it's assigned to.

Did you do what @aldonaletto told you to do in the Control Point Script as well?

 var go2 = GameObject.Find("test$$anonymous$$ainBuilding");

Hope that helps, $$anonymous$$lep :P

avatar image aldonaletto · Apr 07, 2012 at 01:22 PM 0
Share

@$$anonymous$$leptomaniac is right: both scripts should be fixed, as I told at the end of my answer. But the whole thing is too complex: flags that set/clear other flags, that are used in another script to modify a value that's read back by the first script in Update, and with OnGUI and Collision events interlaced... I think you should make a flowchart, then rewrite the whole thing! And pay attention to the error messages: you should have at least 3 of them with the original code.

avatar image PerfectOrigin · Apr 07, 2012 at 03:23 PM 0
Share

ok thanks everyone for being so helpful! Ill re-write the script and make it less complex.

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

Setting Scroll View Width GUILayout 1 Answer

How do I correct the following error on the Lerpz Tutortial GUI 1 Answer

Tracking Down GUI Errors 0 Answers

"NullReferenceException" while trying to draw a texture (C#) 1 Answer

I keep getting errors please help this script is to make gui appear if trigger is on 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