Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TravisF117 · May 20, 2016 at 05:34 PM · script.variablessharing

How to connect two c# scripts and connect varibles?

I have attempted to connect a variable from one c# to another and I can't figure out were I have gone wrong. The premise of my game is to get to a checkpoint before the timer runs out and when you have hit said checkpoint the timer resets, can someone please help? Here is my code;

 using UnityEngine;
 using System.Collections;
 
 public class onTrigger : MonoBehaviour {
 
     void OnTriggerExit(Collider other)
     {
         GetComponent<Timer>().m_Time = 5f;
     }
 
     /*
     IEnumerator Wait()
     {
         yield return new WaitForSeconds(1);
     }
     */
 }

 public class Timer : MonoBehaviour
 {
 
     public Slider m_Slider;
     public float m_Time;
     public float m_StartTime = 5f;
 
 }

I am trying to share the variable m_Time. Thank you

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 NoseKills · May 21, 2016 at 02:50 PM 0
Share

You should tell us why you think you have gone wrong. Do you get an error? Or doesn't the value just change? Those scripts don't have anything wrong with them as such I believe.

1 Reply

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

Answer by KazYamof · May 20, 2016 at 07:13 PM

I will assume you are not using any design pattern like Factory, Singleton, etc. and are no worried about everything be public on your class. So just to demonstrate, you can do something like this:

 public class TriggerManager : MonoBehaviour { //avoid onTrigger as class name, sounds like a Event name
 
      public Timer myTimer; //field of the class Timer
 
     void Awake()
     {
           this.myTimer = Find("TimerGameObjectNameHere").GetComponent<Timer>(); //try to find the object
           if (this.myTimer == null) //null check, if there is not a Timer instantiated, create one and assign the reference to the local field
                this.myTimer = new GameObject("TimerGameObjectNameHere").AddComponent<Timer>();
      }
 
      void OnTriggerExit(Collider other)
      {
          this.myTimer.m_Time = 5f;
      }    
  }
 
 //still the same as yours
 public class Timer : MonoBehaviour
  { 
      public Slider m_Slider;
      public float m_Time;
      public float m_StartTime = 5f; 
  }

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 TravisF117 · May 21, 2016 at 07:59 PM 0
Share

Never $$anonymous$$d, I forget to assign a object, thank you very much for your help.

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

assign a new value to a variable 0 Answers

Script error and no Variables showing 0 Answers

How to compare two different variables in two different scripts? 0 Answers

How can I access to child objects variables from other C# script ? 0 Answers

Invoking a Function From A Variblized Script And Object 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