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 bender · Jan 04, 2012 at 01:11 PM · class

function Start() and class in class

Hi,

I'm new in unity3d and i'm doing something wrong. How to use class in class? I write code like this and i can't initiate class Player with values in Stat class fields.

 var player : Player;

 class Stat{
     var current: int;
     var max: int;
 }

 class Player{
     var name: String;
     var hp: Stat;
     var mana: Stat;
     
     var attack: int;
     var defense: int;
     
     function Player(name: String, hp: int, hpMax: int, mana: int, manaMax: int, attack: int, defense: int){
         this.name = name;
         this.attack = attack;
         this.defense = defense;
         
         this.hp.current = hp;
         this.hp.max = hpMax;
         
         this.mana.current = mana;
         this.mana.max = manaMax;
     }
 }

 function Start(){
     
     player = new Player("PlayerName", 10, 20, 10, 20, 99, 99);
 }

How to set values to hp.max, hp.current, mana.max and mana.current? How to do it right?

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

3 Replies

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

Answer by Eric5h5 · Jan 04, 2012 at 01:33 PM

You need to create a new instance of the class before you can use it; by default it is null. In the same way that you're creating a new instance of Player when you make the player variable.

Comment
Add comment · Show 2 · 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 Bunny83 · Jan 04, 2012 at 03:56 PM 0
Share

Just in case you don't understand this answer, the hp and mana variables are classes. Classes are always reference types so you have to create an instance before you can use it.

so in your Player constructor do

 this.hp = new Stat();
 this.mana = new Stat();

before you access the variables of these objects.

Another way would be to change the Stat-class into a struct

 struct Stat{
     var current: int;
     var max: int;
 }

A struct is a value type so it has always something assigned. It can't be null. It's not a reference to another object, it is made of it's variables

avatar image Eric5h5 · Jan 04, 2012 at 05:49 PM 1
Share

The struct keyword doesn't exist in Unityscript; to make a struct you have a class extend System.ValueType.

avatar image
-1

Answer by jakovd · Jan 04, 2012 at 02:16 PM

You need to make these variables public or make new methods for changing their values. Take a look at public keyword on wikipedia: http://en.wikibooks.org/wiki/Java_Programming/Keywords/public

This is "basic programming stuff" and you'll probably use this in every script you write.

Comment
Add comment · Show 2 · 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 Eric5h5 · Jan 04, 2012 at 02:35 PM 1
Share

They are already public; that's not an issue.

avatar image Bunny83 · Jan 04, 2012 at 04:00 PM 0
Share

Yep, it seems you mix up Java with JavaScript which are two different languages. In UnityScript (Unity's Javascript) everything is public by default. In C# (and Java, which is quite similar to C#) everything is private by default.

avatar image
-1

Answer by Jenzik · Jan 04, 2012 at 03:50 PM

 function Start(){
 player = new Player(); // You need to Make a new Instance of the class before assigning values to it =)
 player = new Player("PlayerName", 10, 20, 10, 20, 99, 99);

}

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 Bunny83 · Jan 04, 2012 at 03:58 PM 1
Share

This doesn't make any sense. You create a Player object with the default constructor and then you create another one (with the overloaded constructor) and throw the first one away.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Parent Class variables in Child Class's Inspector (C#) 0 Answers

Field is never assigned to, and will always have its default value 3 Answers

Collisions and Lists 1 Answer

How can I access this class from another script? 1 Answer

Creating a preset class that can be assigned in the inspector 0 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