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 GodOfTheFools · Oct 25, 2012 at 05:30 PM · functiontimerscorecall

Why can't I call function from another script?

Hi people, just wondering if you could have a look at my script. I'm trying to call over a function from another script and it keeps giving me the error that it is not a member of "Object".Here's my scripts

Score Script:

 function CheckValueOfScore(){
     //Debug.Log("Score equals "+score);
     if (score > 299)
     {
     
     waterMarkObject = GameObject.FindWithTag("GUITime");
     countdownScript = waterMarkObject.GetComponent(Countdown);
     currentGUITime = countdownScript.HandOverGUITime();
     Debug.Log("ShootScript reads GUITime as "+currentGUITime);
     
     
     Debug.Log("Countdown reading as "+countdownScript);
     
     
     yield WaitForSeconds (3);
     Application.LoadLevel(3);
     Debug.Log("Game over");
     } 
 }

Timer script

 var startTime : int;
 private var restSeconds : int;
 private var roundedRestSeconds : int;
 private var displaySeconds : int;
 private var displayMinutes : int;
 var countDownSeconds : int;
 var Text;
 var customGuiStyle : GUIStyle;
 var GUITime :int;
 
 function Awake() 
 {
     startTime = Time.time;
 }
 
 function OnGUI()
 {
 Debug.Log("Real GUITime is "+GUITime);
 GUITime = Time.time - startTime;
     restSeconds = countDownSeconds - GUITime;
 
     //display messages or whatever here -->do stuff based on your timer
         if (restSeconds == 60) 
         {
            print ("One Minute Left");
            }
             if (restSeconds == 0) 
             {
         print ("Time is Over");
         Application.LoadLevel(2);
         //do stuff here
         }
 
     //display the timer
             roundedRestSeconds = Mathf.CeilToInt(restSeconds);
         displaySeconds = roundedRestSeconds % 60;
         displayMinutes = roundedRestSeconds / 60; 
 
         Text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds); 
         GUI.Label (Rect (400, 25, 100, 30), "Time Remaining: " +Text, customGuiStyle);
     
         }
   
     
 function HandOverGUITime(){
     Debug.Log("inside HandOverGUITime()"); 
     return GUITime;
  }  

Any help would be lovely :D

Comment
Add comment · Show 7
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 Xroft666 · Oct 25, 2012 at 05:48 PM 0
Share

Why is it "Timer" script if you are trying to get "Countdown" script?

avatar image phodges · Oct 25, 2012 at 05:55 PM 0
Share

As an aside, please note that OnGUI will be called more than once in a frame, so your 'GUITime' timestamp may not have the meaning that you are intending. Personally, I would avoid updating state in OnGUI and ins$$anonymous$$d only use it for layout and display purposes.

avatar image Memige · Oct 25, 2012 at 06:12 PM 0
Share

Where is your variable Countdown declared? And your countdownScript variable, can you confirm the type?

avatar image GodOfTheFools · Oct 25, 2012 at 08:00 PM 0
Share

@Xroft666: Sorry about that, it is in fact called Countdown, that was an error on my part. Apologies.

@phodges: The final intention of this script is to call the time remaining and then times that number by ten to add to the final score so wouldn't it have to be on GUITime or am I mistaken?

@$$anonymous$$emige: Bugger, me and my lecturer hadn't noticed that the variable countdown isn't declared. Also, could you tell me how to check the type of a variable? Won't know if not declaring countdown is the workaround but if so will let you all know tomorrow when I have access to me work.

In the meantime, any more helpful hints/ideas? =]

avatar image phodges · Oct 25, 2012 at 08:09 PM 0
Share

You could use an Update function to handle tracking state and changing level, reserving OnGUI for display. Since you only seem to care about the time remaining, you could add an explicit variable (i.e. var timeRemaining : float), which would be initialised at the beginning of the round and decremented using Time.deltaTime.

Show more comments

1 Reply

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

Answer by Sonaten · Oct 25, 2012 at 09:27 PM

To address the question, even though it seems that you are getting a lot of good hints on 'other' topics. :)

To call a function in ScriptA from another ScriptB you will have to let ScriptA know about ScriptB. Like so in ScriptA:

 public var scriptKeeper : SctiptB;
 
 function Start ()
 {
     scriptKeeper = GameObject.Find("NameOfObjectWithScriptB").transform.GetComponent<"ScriptB">();
 }

You would then be able to use

 scriptKeeper.wantedFunctionName();

from within ScriptA, provided that the function is public.

I admit that I did not take the time to look through your scripts closely, thus from the question title this seemed to fit as a posible solution.

I hope that this can help you out a bit.

Comment
Add comment · Show 2 · 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 Eric5h5 · Oct 28, 2012 at 02:46 AM 0
Share

That's incorrect syntax, and GetComponent works on game objects, not transforms.

 script$$anonymous$$eeper = GameObject.Find("NameOfObjectWithScriptB").GetComponent(ScriptB);
avatar image Pias108 · Dec 28, 2013 at 07:36 AM 0
Share

Thanks Eric5h5 :)

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

15 People are following this question.

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

Related Questions

Building a 3D Neural Network for Bot AI? 1 Answer

How to call a function from another script without referencing it? 1 Answer

Calling a Function from other script ( C# ) 1 Answer

Call function in another script? 1 Answer

How to reference another script and call a function in C# ? 2 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