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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Static Variables between Scenes 1 Answer

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

Prefab's Script affecting all the Prefab 1 Answer

Can't access a javascript static variable from c# script 1 Answer

How to check if variable exists? 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