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.
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;
}
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
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;
}
}
Answer by corallotwins · Jun 09, 2016 at 11:21 AM
by the way I forgot to mention i'm trying to make OOP Encapsulation.
Your answer
Follow this Question
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