• 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 mazagame · Mar 17, 2015 at 03:49 PM · variablefloatassemblyanother script

The type or namespace name `timeLeft' could not be found. Are you missing a using directive or an assembly reference?

Hello Unity answers community, today I present you with the opportunity to help another newbie like myself access a float variable from one C# script in another C# script. I've spent countless hours researching the gameObject.GetComponent methods, and have seen a plethora of examples provided here on the answers forums and elsewhere on the web. Yet for some reason unbeknownst to me, even after numerous tweaks to both of my two scripts, I still can't manage to get the game running. The title above describes the error I am currently receiving.

Anyway, here is the logic behind my two scripts. The first script, seen below, is a simple countdown timer script that uses the public float variable "timeLeft" to countdown from a set value; this one functions as it should. The second script I created was intended to be one that was activated on trigger enter. The script could be placed and used universally on numerous triggers, these object triggers would either add or subtract time based upon their significance in the game, then the object would destruct itself in order to prevent exploits.

Here are my two scripts:

Timer.cs

 using UnityEngine;
 using System.Collections;
 
 public class Timer : MonoBehaviour {
     public float timeLeft = 15.0f;
  
     public void Update() {
         timeLeft -= Time.deltaTime;
  
         if (timeLeft <= 0.0f) {
             Application.LoadLevel(1);
         }
     }
 
     public void OnGUI() {
         GUI.Box(new Rect(600, 10, 100, 20), "Time Left: " + (int)timeLeft);
     }
 }


ModifyTimer.cs

 using UnityEngine;
 using System.Collections;
 
 public class ModifyTimer : MonoBehaviour {
     public GameObject Timer;
     public timeLeft TimerScript;
 
     public float addTime = 0f;
     public float subTime = 0f;
 
 
     void Awake () {
         //Reference the timeLeft variable in the Timer script
         Timer = GameObject.FindGameObjectWithTag(Tags.timer);
         TimerScript = Timer.GetComponent<timeLeft>();
     }
 
     void OnTriggerEnter (Collider other) {
         //Either add or subtract time to timeLeft based on variable input
         if (addTime >= 1) {
             Timer.timeLeft += addTime;
         }
         if (subTime >= 1) {
             Timer.timeLeft -= subTime;
         }
         Destroy(gameObject);
     }
 }

So why can't the timeLeft variable be modified in this way? I have tried so many different methods of accessing the timeLeft variable from the first script to no avail. Any help is greatly appreciated, please go easy on me as I just recently started working in Unity with C# as a student.

Comment
Add comment · Show 3
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 gjf · Mar 17, 2015 at 02:52 PM 0
Share

GetComponent gets the component (script in this case) so you'd do this:

 TimerScript = Timer.GetComponent<Timer>();

then reference the time left via TimerScript.timeLeft

avatar image emayberry · Mar 17, 2015 at 05:59 PM 0
Share

I agree with @AlwaysSunny, and I think he should make his comment into an answer

avatar image mazagame · Mar 18, 2015 at 12:01 PM 0
Share

I appreciate your help @AlwaysSunny, the script you offered works exactly as I would have hoped. The modify timer script actually looks a lot simpler than I could have ever imagined it being. I can confirm that your comment acts as an accurate answer.

1 Reply

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

Answer by AlwaysSunny · Mar 17, 2015 at 03:01 PM

I'm afraid this code is a bit "all over the place" in terms of sensibility. I think I understand the description of your desired outcome though, so kudos for that. Good plain English goes a LONG way to getting things done around here.

Seems like you want a single instance of your Timer script, and you'd like other scripts to have access to that single instance. There are a few ways to go about this, but to keep things simple, let's do this:

Keep your Timer script. It looks okay to me.

Replace your ModifyTimer script with this:

 using UnityEngine;
 using System.Collections;
 public class ModifyTimer : MonoBehaviour {
 public Timer timer;
 
 // this can be positive to add time, or negative to subtract time
 public float timeModifier;
 
 void OnTriggerEnter() {
   if (timer!=null) {
     timer.timeLeft += timeModifier;
     // make the object this script is on destroy itself with
     // GameObject.Destroy(gameObject)
     // or make the script self-destruct, leaving the object behind with
     // Destroy(this);
   }
 }

Then, when you attach this script to an object with a trigger, you'll see the "timer" variable exposed. Drag whichever object holds your Timer instance onto this field to assign the reference.

We were all green once, no worries. ;)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Monodevelop files a compiler error when I try to make a variable,Monodevelop files complilerr error when I try to make a public variable 1 Answer

What does it mean when you type a variable to float? 1 Answer

JS Setting int value from another script 1 Answer

Decrease life from another script 1 Answer

Does it matter to declare variable type? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges