Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
5
Question by clay53_clayton · Dec 30, 2015 at 07:05 PM · script.textreferenceunity 4.6access

[Help] how do I reference/access another script in unity C#

I am making a text adventure game, but I don't know how to sync variables from one script to another also please give a good explanation on how to do it.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
39

Answer by antiquote · Dec 31, 2015 at 07:02 AM

First thing's first. Computers can not make assumptions, so we must define everything within in the computers abilities.

Almost everything that can be referenced follow the same basic syntax in C#.

 //[scope] [type] [name]
 
     public float velocity = 10f;
 
     private int money = 10;
 
     protected string dairy = "cheese";
 
     public float Velocity { get{} set{} }
 
     private int Money { get{} set{} }
 
     protected string Dairy { get{} set{} }
 
     public void Velocity () {}
 
     private int Money () {}
     
     protected string Dairy () {}
 

Everything listed here is interchangeable excluding the syntax. What you want to focus on first of all is the scope which defines where your Method, Variable, or Property can be accessed.

Scope

  • public - Can be accessed by any and all scripts just as long as you have a reference to the script where the variable, property, or method is created.

  • protected - Can be accessed ONLY by inherited members, a good example of inheritance is:

    public class MyScript : MonoBehaviour {

    }

Almost all of your game scripts in Unity will inherit MonoBehaviour allowing you to use everything MonoBehaviour owns, however. If you didn't inherit it, it won't be possible to interact with your game as many of the Methods, Variables, and properties are protected. Like only you and the people living in your home have access to the attic.

  • private - Can not be accessed from anywhere, other than the class it was created in. Period.

Now that we got that squared away, creating a reference is quite easy, and there's a bunch of ways to do it.

If you're trying to reference a script that's attached to the same GameObject that you're referencing from all you have to do is GetComponent.

 //Creating and assigning the reference variable.
 
 ReferencedScript refScript = GetComponent<ReferencedScript>();
 
 //Creating then assigning the reference variable.
 
 ReferencedScript refScript;
 
 refScript = GetComponent<ReferencedScript>();

If you're referencing a script from a different GameObject, you much reference the GameObject first, then reference the script.

 //Without reference variable
 GameObject objectWithScript;
 
 objectWithScript.GetComponent<ReferencedScript>();
 
 //With reference variable
 GameObject objectWithScript;
 
 objectWithScript.refScript;

Once you have referenced your script, you can access anything within it that's been set to public. Even assign variables and properties from your the script you're referencing from or call methods.

 //From Same GameObject
 GetComponent<ReferencedScript>().velocity = 4f;
 
 refScript.Velocity = 5f;
 
 refScript.Velocity();
 
 //From Seperate GameObject
 objectWithScript.GetComponent<ReferencedScript>().velocity = 4f;
 
 objectWithScript.refScript.Velocity = 5f;
 
 objectWithScript.refScript.Velocity();
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 MO-NASSER · May 25, 2020 at 04:44 PM 0
Share

if the script doesn't attach to any game object ??

avatar image gaming_man_is_gaming MO-NASSER · Mar 24, 2021 at 07:01 PM 1
Share

attach it to a game object not related to anything and just reference that

avatar image
0

Answer by Pie556 · Oct 23, 2021 at 12:28 AM

yes you could just attach that script to that gameObject and also I do believe u can make that script static just get access to that script (In the same script) and make it static and by looking at your code i believe you know what a static variable is and yeah

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

44 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 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 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

Why isn't my C# code sending updates to my text scoreboard? 1 Answer

Basic Unity: Can someone explain the referencing and instantiation process? 1 Answer

Score wont display 0 Answers

Text File Show Problem In Android Apk 0 Answers

How to display text with delay (No GUI code in script) 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