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 /
  • Help Room /
avatar image
0
Question by corallotwins · Jun 08, 2016 at 08:20 PM · c#oopclass objectclass instance

Information about Classes and Instantiate Objects C#

Hi to all Unity Experts, I was wondering how can I instantiate Objects from Classes. and before I start sorry for my Bad English.

well here we go I'm trying to create a class named "Enemies" this is the class Enemies

 using UnityEngine;
 using System.Collections;
 
 public class Enemies : MonoBehaviour {
 
 private int hpEnemy;
 
 public int _hpEnemy
     {
 
         get { return hpEnemy; }
         set { hpEnemy = value; }
 
     }
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
 

yeah I know there's no sense mistakes lol, but I'm trying to studing C# and Unity al by myself.

and when I try to open another Script Often in Visual Studio C# i do this

 Enemies myFirstEnemy = new Enemies();         // <---Instantiate an Object
 myFirstEnemy._hpEnemy;                              //  <-----Calling the propriety

but the problem is that in Unity when I try to Instantiate the Object, it doesn't give me the proprieties like above :\

I would like to know why it doesn't work, I want an explaination :(

Again Sorry for my Bad English.

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
3
Best Answer

Answer by TBruce · Jun 08, 2016 at 09:18 PM

First you can not call new on a MonoBehaviour. A MonoBehaviour must be added to a GameObject. You either directly add it to a GameObject in the inspector or you must do it by calling GetComponent(); in script. So you could do something like this

 public Enemies enemies;
 
 void Start()
 {
     // was a enemies property set in the inspector?
     if (enemies == null)
     {
         // no - so lets add one to the Current GameObject
         enemies = GetComponent<Enemies>();
     }
     enemies._hpEnemy = 0;
 }
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 corallotwins · Jun 08, 2016 at 09:31 PM 0
Share

Thank you very much it worked :D another question when you said

  // was a enemies property set in the inspector?
  if (enemies == null)

do you mean if the property were Public? or what do you mean

avatar image TBruce corallotwins · Jun 08, 2016 at 10:05 PM 0
Share

That was just an example.

When I did this

 public Enemies enemies;

I made a property visible in the inspector which allowed you to set it there. It might have well been a private.

But if it was not set in the inspector then the value would be null an you would get an error when you tried to use it. That is the reason for this

 // if the property was not set in the inspector the try and get the component if one is attached
 if (enemies == null)
 {
     // no - so lets add one to the Current GameObject
     enemies = GetComponent<Enemies>();
 }

But that is only good if one is attached. What if it was not set in the inspector and one was not attached? This will give you an error

 enemies._hpEnemy = 0;

You can always do this before trying to access enemies

 if (enemies == null)
 {
     // enemies is null so lets add one
     enemies = AddComponent<Enemies>();
 }

Then you can go another route

 // make sure that an Enemies has been added to this GameObject
 // this statement will attach an Enemies component if one is not attached
 [RequireComponent(typeof(Enemies), typeof(Enemies))] 
 public class $$anonymous$$yclass : $$anonymous$$onoBehaviour
 {
     private Enemies enemies;
 
     void Start()
     {
         // because of the RequireComponent statement above there is no need to check if there is one
         enemies = GetComponent<Enemies>();
         enemies._hpEnemy = 0;
     }
 }
avatar image
0

Answer by corallotwins · Jun 09, 2016 at 11:21 AM

by the way I forgot to mention i'm trying to make OOP Encapsulation.

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

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

Creating a copy of an existing class object 2 Answers

OOP Conceptualization in Unity 1 Answer

How to modify a 2d array [,] of a class? 1 Answer

Custom class, Null Reference Exception 4 Answers

IAP function not calling 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