Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RandomUser123 · Jan 25, 2016 at 11:40 AM · classobjectsreference

Using accessors and mutators from another class

I'm trying to learn about classes and how they can be used within Unity, I have the following code for one of the objects in my scene:

 using UnityEngine;
 using System.Collections;
 
 public class WorkerStats : MonoBehaviour
 {
     public class Stats
     {
         public string workerName;
         public int level;
         public int health;
         public int resources;
 
         public Stats()
         {
 
         }
 
         public Stats(string nameParam, int levelParam, int healthParam, int resourcesParam)
         {
             workerName = nameParam;
             level = levelParam;
             health = healthParam;
             resources = resourcesParam;
         }
 
         public void SetName(string nameParam)
         {
             workerName = nameParam;
         }
 
         public string GetName()
         {
             return workerName;
         }
 
         //I have get and set functions for all variables
     }
 
     void Start()
     {
         Stats myStats = new Stats();
 
         myStats.SetLevel(1);
         myStats.SetName("Benny");
         myStats.SetHealth(100);
         myStats.SetResources(0);
     }
 }

These will define the object that they are attached to, say I wanted to use one of these functions from another class, for example, increasing the workers level by one.

For instance, I can imagine it would be something like this, now I know this is wrong but would i be something alone these lines?

 public class IncreaseWorkerLevel : MonoBehaviour
 {
     public WorkerStats ws;
     
     void Start()
     {
         ws.myStats.SetLevel(myStats.GetLevel() += 1);
     }
 }




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

2 Replies

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

Answer by Garazbolg · Jan 25, 2016 at 02:23 PM

Not quite but you are close.

The first error is that you declare your field myStats in the start function so as soon as your thread leaves it you will have lost myStats. You need to declare it as a field of WorkerStats :

 using UnityEngine;
  using System.Collections;
  
  public class WorkerStats : MonoBehaviour
  {
      public class Stats
      { 
          /* your class code here */
      }
     
      public Stats myStats;
 
     void Start()
     {
 
          myStats = new Stats("Benny",1,100,0);
 
     }
 }

Then you can access it the way you did.

Also be carefull, you're trying to assign a value to a getter :

 ws.myStats.SetLevel(myStats.GetLevel() += 1);

It will work but i think it will get you a warning. Just use a simple '+'. Or you could use Unity built-in getter/setter system to do just :

 ws.myStats.level += 1;

And still control what the user put in.

Hope it helps you understand more about Unity Scripting. I'm guessing you're coming from C++. Well it isn't much different but you should find some tutorials on the internet on how to script for unity, there is a ton of it.

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 RandomUser123 · Jan 25, 2016 at 03:23 PM 0
Share

Ah yes, that makes sense. I tinkered around a bit and managed to get it working using your advice, I'm also no longer trying to assign a value to the getter, mistake on my part there :P I shall look into Unitys' built in getter/setter system.

Thanks for the help @Garazbolg

avatar image
0

Answer by coolraiman · Jan 25, 2016 at 03:55 PM

if you are already experienced with c#, the first thing to learn in unity is how to make script interact with each others

look at the object in your scene with the component (everything is a component)) IncreaseWorkerLevelk.

you should see in the inspector once the object is selected : Increase Worker Level and jsut under that, a public field named ws with a rectangle next to it. try to slide your object with the component Worker Stats in that rectangle. A reference is now made.

if you want to make a reference at runtime you can use in your start function or anywhere else:

ws = GameObject.Find("nameOfTheObjectWithTheWorkerStatsComponent").GetComponent();

from these basics, you should be able to play with how script interact each other

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

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

Add all objects of a type to a list before runtime 1 Answer

Splitting with Partial Classes 0 Answers

Creating a pointer variable to a GameObject inside a class that does not extend MonoBehavior? [C#] 2 Answers

UnityScript - Class reference 1 Answer

I have a lot of references 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