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
1
Question by Attapi0 · Dec 01, 2013 at 08:29 PM · variablesreferencing

Instance(?) problems

This is a REALLY dumb question.

So I made a script on a cube called Stats.js. Stats.js sets the stats for that "Creature" in the game.

 #pragma strict
 
 
 public var EnemyName = "Rat";
 public var EnemyHealth = 10;
 public var EnemyAttack = 5;
 public var EnemyDefense = 0;
 public var EnemyBurn = 0;
 public var EnemyFreeze = 0;
 public var EnemyShock = 0;
 

When you initiate a battle, it executes a script called EnemyFight.js. This script tries to reference the Stats.js variables

 #pragma strict
 
 if (Stats.EnemyName === "Rat"){
 
     print("My hand is a dolphin");
 
 }


But I get an error that says "BCE0020: An instance of type 'Stats' is required to access non static member 'EnemyName'."

I've tried a lot of things.I'm not the sharpest tool in the shed so please babify the explanation if necessary.

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 static_cast · Dec 01, 2013 at 09:57 PM 0
Share

You program like I do. Outputting random statements as debug. [Recognize the reference though, Friday by Brock's Dubs]

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CHPedersen · Dec 01, 2013 at 08:47 PM

The error is telling you that you're trying to statically access the class member called "EnemyName", but it can't, because that variable is not marked static.

A static variable is one which does not require an instance of the object to be accessed; it is instead globally available and has but one value across the entire scope of the application. A typical example of a static variable (in this case a property) is Vector3.up. It requires no instance of the Vector3 type but is directly accessible and means (0,1,0) everywhere no matter where you call it.

An "object instance" is what you have whenever you create something with the "new" keyword. The "new" keyword invokes an object's constructor and returns an instance of it based on the class's definition. A class may thus be said to be a blueprint of the object. But in this particular case, "Stats" is a script, which just means it is an object of a type that inherits from MonoBehavior. MonoBehaviors are always attached to a GameObject in your scene. Unity takes care of instantiating the object for you in this case, so for scripts, you will never use the new keyword to create instances of them. Instead, you acquire a reference to the object using the GetComponent method:

 Stats yourStatsScript = GetComponent<Stats>();

This line of code returns the Stats script if executed in a script that is attached to the same gameobject as the Stats script. If not, you must run GetComponent on a reference to the gameobject that has the Stats script.

Once you have the object reference, you can access the EnemyName through that instead:

 if (yourStatsScript.EnemyName === "Rat"){ 
     print("My hand is a dolphin"); 
 }

If, in the future, you truly wish for a class member to be static, you simply mark it with the static keyword:

 public static var EnemyName = "Rat";

After this, it will be accessible the way you did in your script, that is, by writing "Stats.EnemyName". But Stats appears to be something that is specific to the individual enemy type, so I'm guessing the EnemyName will not be the same for every instance of the Stats object. Therefore, it should not be static.

Comment
Add comment · Show 3 · 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 Attapi0 · Dec 01, 2013 at 08:58 PM 0
Share

I followed what you said, but I just got a bunch of errors. alt text

EDIT: to clarify, there IS a semicolon at the end of line 3.

EDIT2: so I changed the <>'s to ()'s and that removed most of the () related errors. I'm still getting the damn semicolon error though.

avatar image CHPedersen · Dec 01, 2013 at 09:49 PM 0
Share

Those typically arise from simple syntax errors. Update your question with the full script. If I were to venture a guess, however, I'd say it's probably because you tried to use my C# code as unityscript.

avatar image CHPedersen · Dec 04, 2013 at 02:48 PM 0
Share

Please follow-up on this. We can't help getting this resolved unless you reply with more information about the errors you're getting.

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 can I reference a float variable to a GUI label in another script? 1 Answer

Properly referencing a variable from another script 1 Answer

How to share a variable through multiple copys of the same script 2 Answers

Reference string from another gameobject and not directly from the script itself? 1 Answer

Deserializing a JSON file yields 0 when referencing the variables in another script 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