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
1
Question by Moaty1 · Jul 22, 2014 at 08:41 AM · componentaccess

Changing a variable from one script to another C#

hello , i know this question has been asked but i haven't got any perfect answer yet so , i have a script called "Score" which has an int variable called CurrentScore = 0

now i have another script which i want it to get that component and the variable "CurrentScore" then on a Collision even ( OnCollisionEnter ) it access the variable "CurrentScore" and changes it to 1++ so every collision it adds 1 . thanks everybody and btw im using C#

oh one more thing , i also have created another gameobject which contains a script called "GlobalTimer" which has an int variable called Timer (its a count down timer) i also want with the collision event above to add lets say (int) more 3 seconds to the timer and thanks again

so , i know how to get a component but the problem is i dont know how to get the variable and add a 1++ to it :)

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 Moaty1 · Jul 24, 2014 at 05:59 AM 0
Share

Hello guys , thanks for participating and answering but finally i figured out how to do such a thing so

 public static int CurrentScore = 0; //we make it static so we can access it from any script.
 
 //from script B we simply do this.
 
 //(your class name aka script name) assu$$anonymous$$g its called ScoreG$$anonymous$$
 
 void OnCollisionEnter()
 {
  ScoreG$$anonymous$$.CurrentScore.DoStuff();
 // or in my case
 
 ScoreG$$anonymous$$.CurrentScore += 1;
 }

 

4 Replies

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

Answer by VIPINSIRWANI · Jul 22, 2014 at 01:08 PM

For Accessing any member of class in other class you can do like that.

 public class Score : MonoBehaviour {
     public static Score instace;
     public int CurrentScore;
     // Use this for initialization
     void Start () {
         instace = this;
     }
 
 }

Now you Can simply access Current Score in any script like

 Score.instance.CurrentScore = 10; // Or Whatever

 
Comment
Add comment · Show 3 · 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 beatsaway · Feb 18, 2018 at 12:22 AM 1
Share

NullReferenceException: Object reference not set to an instance of an object

avatar image Laurence_B · Jul 05, 2018 at 07:41 PM 0
Share

It still works and is very helpful! But does it affect the fps? @VIPINSIRWANI

avatar image jon_underwood · Jul 12, 2020 at 04:54 PM 0
Share

In the above @ VIPINSIRWANI meant to write instance on lines 2 and 6 of his first code segment

This is an example of 'singleton' design pattern. Only one instance of CurrentScore can ever exist

avatar image
1

Answer by ironblock · Jul 24, 2014 at 07:32 AM

 MoveTo MT = gameObject.GetComponent<MoveTo> ();
 MT.enabled = false;

this is the prettiest way " MT" will be local this way and wont interfere with other scripts in the same class.

     scriptName SN = gameObject.GetComponent<scriptName> ();
     SN.var= value;

sudo code ^

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 YoungDeveloper · Jul 22, 2014 at 08:45 AM

I explain it very clearly in these two topics: http://answers.unity3d.com/questions/730088/transform-var-between-scripts.html

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 alejo9719 · Dec 18, 2016 at 09:12 PM

I simply don't know why this doesn't work in my code. I have a "Motorcycle" object with a "BackWCollider" child object which has a "MoveBike" script attached to it.

Script in "Motorcycle":

     private float ThrustIncrement;;
     private MoveBike MoveB;
 
     void start()
     {
         MoveB = transform.Find("BackWCollider").GetComponent<MoveBike>();
     }
 
 
     void OnTriggerEnter(Collider coll)
     {
         if (coll.gameObject.name == "NitroModel")
         {
             ThrustIncrement = 0.1f * MoveB.Thrust;
         }
     }

Script in "BackWCollider" (MoveBIke script):

 public class MoveBike : MonoBehaviour {
 
     public float Thrust; //Wheel torque multiplier
 
 ...

I get this error in the ThrustIncrement setting line:

NullReferenceException: Object reference not set to an instance of an object PowerUps.OnTriggerEnter (UnityEngine.Collider coll)

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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

2D Animation does not start 1 Answer

How to access the interface from the last component in the inspector 2 Answers

Errors while accessing components 2 Answers

Access to variables from other script objects 3 Answers

How to access a Component from another Child when current is Destroyed 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