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 PTerto · Sep 25, 2013 at 03:51 PM · variableaccessaccessing

Is it possible to change a variable, into a script not assigned to any game object?

Hey guys, One exemple, i have two scripts, script A is assigned to a empty into my scene, and script B is one script to help many things into the application, but it's not assigned to any game object in any scene, and it has a int variable, and a want modify the value of this variable from my A script, is it possible?

Comment
Add comment · Show 4
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 QuantumCD · Sep 25, 2013 at 03:54 PM 0
Share

Yes, of course it is. If it's a static class, you can just use ClassName.$$anonymous$$yIntField = Value. If it's not a static class, you can just store an instance inside the Script A class and use that.

avatar image PTerto · Sep 25, 2013 at 04:24 PM 0
Share

I've been trying the second way u said, but unity still saying that object reference not set to an instance.

avatar image QuantumCD · Sep 25, 2013 at 04:27 PM 0
Share

@PTerto - That's probably because the class isn't static. In your Script A, you need to make sure you have an instance. In C#, you'd store a field like private ScriptBClassName _variableName and then do something like void Awake() { _variableName = new ScriptBClassName(); } Then when you need to use the code in that class, you would reference it with _variableName.

avatar image PTerto · Sep 25, 2013 at 04:50 PM 0
Share

But, the way u are saying, i'll be creating another element, ScriptBClassName, am I wrong?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Visual Programmer · Sep 25, 2013 at 05:04 PM

If you use a public static variable it can be accessed from anywhere. I believe it still holds true if it is not attached to an object. For example, I can access this float var from any script without it being attached to the same object.

 public static var myFloat:float;
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 Tanshaydar · Sep 25, 2013 at 05:11 PM 0
Share

Yes, when it is static, it holds the value regardless of being attached to a game object.

avatar image
0

Answer by Tanshaydar · Sep 25, 2013 at 04:59 PM

If script B is deciding many things for the game, then it should be something like a Game Controller, which usually is assigned to a empty Game Object in the scene. For example, you create an Empty Game Object and name it as Game Controller and then assign the script to it.

If you don't want to assign the script to any object, then you can make your variables static, so that it should be available like YourScript.staticVariableName and you can use or assign values to it. Like if( YourScript.staticVariableName == anotherValue) or YourScript.staticVariableName = anotherValue;

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 PTerto · Sep 25, 2013 at 05:45 PM

 using UnityEngine;
 using System.Collections;
 
 public class GuiMainMenu : MonoBehaviour {
 
     private string keySound = "AUDIOGAME";
     public AudioSource click;
     public  GeneralGameSound  x;
 void Start () 
         { 
          x = GetComponent("GeneralGameSound") as GeneralGameSound;
         }
 
 x.teste +=1;
 
 void Update(){
 }




This was the A script, and this

 using UnityEngine;
 using System.Collections;
 
 public class GeneralGameSound : MonoBehaviour {
 
     
     
     public int test = 10;
     private static string x = "GONE";
         
     public static void setTeste(int at)
     {
         PlayerPrefs.SetInt(x,at);
         PlayerPrefs.Save();
     }
     
     public static int getTeste()
     {
         return PlayerPrefs.GetInt(x,0);
     }


is the B Script, what am i doing wrong?

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 Visual Programmer · Sep 25, 2013 at 06:30 PM 0
Share

It looks like you need to change test to public static int test

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

18 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

Related Questions

How to access gameObject variable script 2 Answers

Can't access variable from another script. 1 Answer

How do you access a script if you don't know it's name? 5 Answers

My script accessing a variable from another script recieves an error 1 Answer

Access and Change variables from other Scripts? 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