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 JadeTheFlame · Aug 20, 2014 at 05:58 PM · objectreferencestatic

Understanding static methods and object referencing.

Pretty new to c#, but I'm trying to store all of my variables that I use within a level, in one script. The script look like this:

 using UnityEngine;
 using System.Collections;
 
 public class LevelScript : MonoBehaviour {
 
     public int loadingCount;
     public int loadingCountGround;
     public int boardChunkCount;
     public int sceneBoundHeight;
     public int sceneBoundWidth;
     public Vector3[] GroundWallPosition;
 }

To my understanding, I have not declared anything as static. However, I get the 'An object reference is required for the non-static etc.' from other scripts that modify the variables. Such as:

 LevelScript.loadingCountGround ++;

Could anyone break down the use for static variables, why would I need them, and is there a good solution to modify and call upon other variables from other scripts that doesn't use static variables?

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 JadeTheFlame · Aug 20, 2014 at 08:06 PM 0
Share

Ok so after taking advice, the main problem now resides in my Vector3[]. When I make it static, then my line of code:

 foreach (Vector3 wallPosition in LevelScriptCheck.GroundWallPosition)

Throws me an error..."Static member `LevelScript.GroundWallPosition' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d"

3 Replies

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

Answer by $$anonymous$$ · Aug 20, 2014 at 06:19 PM

static variables are used for non object specific things, like in a game where every gun uses the same ammo... there would be a static int called ammo, this means no matter what object is active when the ammo is decreased or increased it does it for every weapon. what the error means is that you have not selected what script you want to change IE if every weapon had different ammo, id need to select what weapon i want to change the values of,

in your case you want to increment LevelScript.loadingCountGround but to do this your need a reference to that scrip Like so,

 public LevelScript TheScript;
 
 void Thefunction
 {
     TheScript.loadingCountGround++
 
 } 
 

sorry if this doesnt make much sense, please message back if it doesnt and i will try and explain better :D

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 JadeTheFlame · Aug 20, 2014 at 07:59 PM 0
Share

I tried this, and after replacing many lines of code in 10+ scripts, unfortunately it gives me the same error... except en mass.

avatar image
0

Answer by smoggach · Aug 20, 2014 at 06:21 PM

LevelScript.loadingCountGround

This line is assuming that loadingCountGround is a static public variable.

LevelScript is the class name. You need to modify an INSTANCE of LevelScript. To get that instance you can use GetComponent();

So in conclusion to access the variables in your code you need to use GetComponent().loadingCountGround++;

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 JadeTheFlame · Aug 20, 2014 at 07:58 PM 0
Share

GetComponent(???).loadingCountGround++ Sorry you'll have to put this in laymans terms. What exactly is the context here? And how would I modify an instance of a script? There's only one version at any given time, can't the engine just identify it?

avatar image
0

Answer by Pecek · Aug 20, 2014 at 06:24 PM

Static means it will be the same value on every instance, that's why you don't have to specify it, it's good when you want only one instance(usually audio/network/etc manager).

Public means you can modify that value outside of the script(common usage for public values are health modification - a bullet can modify the enemy's health).

In your case you can declare your variables as "public static" and the

 LevelScript.loadingCountGround ++;

will work, or you have to specify the instance of the class either by declaring it as a public value and drag it in the inspector or by searching trough the gameobjects to find an instance of it. In this case static variables are a better option in my opinion since you probably don't want to have multiple instances, this way you can keep your hair when a few weeks later everything stops working because you accidentaly draged an another instance to the scene.

Btw search for the singleton pattern, you probably will find a much more clear answer to this problem.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Do you have to set a object reference for every script? 0 Answers

Object Reference Exception (I'm getting sick of these) :P 1 Answer

C# Null Object Reference - Driving me mad! 1 Answer

Get variable from seperate script. 1 Answer

object not set to instance? 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