• 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
Question by Zimmel110 · Sep 04, 2014 at 05:27 PM · c#variablechangestatic

Change static variable in JS through Csharp

Hi. I am working on a script to change a variable of a JS script through a C#

my JS:

 static var counter : int = 10 + counter2; 
 static var counter2 : int = PlayerPrefs.GetInt("CubeCoin");
 
 function Update(){
 CubeCoin.counter = 10 + counter2;
 
 if(CubeCoin.counter2>=0){
 CubeCoin.counter2=0;
 }
 }

my C#

 using UnityEngine;
 using System.Collections;
 
 public class CSharp2 : MonoBehaviour 
 {
     //create a variable to access the JavaScript script
     static CubeCoin jsScript; 
 
     void Awake()
     {
         //Get the JavaScript component
         jsScript = this.GetComponent<CubeCoin>();
     }
     

     void OnGUI()
     {
         if (GUI.Button (new Rect (15, Screen.height-Screen.height/5, Screen.width/6, Screen.height/13), "bla")) {
             jsScript.counter2 +=1;
         }
     }
 }
 


but this don`t work. i got error CS0176 Static member CubeCoin.counter2 cannot be accessed with an instance reference, qualify it with a type name instead. how can i fix that ? and i have to leave the variable static.

Comment

People who like this

0 Show 0
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

1 Reply

  • Sort: 
avatar image

Answer by Graham-Dunnett · Sep 04, 2014 at 05:35 PM

A static member belongs to the class itself, and not to a particular object that has that type. So, it's incorrect to try an access a particular instance of that class through GetComponent(). Instead, you need to use the class:

 // replacement for line 19
 CubeCoin.counter2 += 1;

If you are not familiar with static members, take a look at the GameObject class:

http://docs.unity3d.com/ScriptReference/GameObject.html

Lots of the variables apply to regular game objects in your scene. Near the bottom of the page is a section titled "Static Functions". This section describes the static functions of the GameObject class. If you look at the documentation for the Find() function you'll see that you use GameObject.Find() - that's to say you use the class name and not a variable set to a particular game object.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

how to acess Static variable in other scripts without extended functions? 2 Answers

Static Variables between Scenes 1 Answer

Specific enemy variable affects all enemies 2 Answers

Where is the Mistake in my Coin System? 1 Answer

Unity how to access a static variable from the FirstPersonController component 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