- Home /
Assigning a GameObject to a class
Going to go ahead and re-ask this question, because on my old thread everyone kept commenting on the first answer which was irrelevant to the quesiton... Anyway, my goal is to create a NPC System without using MonoBehaviour, so I will be creating NPCs via script, and using Instancing to get all of their stats... Here is a basic script for getting the stats for the NPC.
public class NPCs
{
public NPC[] npcList;
public int npcId;
void createNPC()
{
npcId = npcId + 1;
npcList[npcId].maxHealth = 100;
npcList[npcId].CurrentHealth = npcList[npcId].maxHealth;
npcList[npcId].expReward = 50;
}
public class NPC
{
public int currentHealth {get; set;}
public int maxHealth {get; set;}
public int expReward {get; set;}
}
}
What I'm trying to figure out how to do is bind the NPC class to the created GameObject, because With this current script I can "Create" a NPC and Modify it's values, although there is no visual game object. For this I need to create it. The reason this is so important is I am going to combine it with my targetting system and use it to grab these values. I'm thinking about putting a GameObject in the npc class, and defining it in the createNPC constructor.... but I'm not sure yet.
Answer by ryba · Jan 23, 2013 at 01:56 PM
Note: using MonoBehaviour would be best solution here, it doesn't cost u anything and provide much simplification for your code. But assuming there is really important reason why you cannot use that (i cannot imagine such, but well ... its up to you):
Best would be to create static Dictionary, that will map game objects to NPC class instances, like that:
public class NPCs {
public Dictionary<GameObject, NPC> npcList = new Dictionary<GameObject, NPC>();
void createNPC() {
// Instantiate game object here
GameObject npcGameObject = InstantiateFunction(); // This function must create GameObject, for example instantiate prefab, and return it
NPC npc = new NPC();
npcList.Add(npcGameObject, npc);
npcList[npcGameObject].maxHealth = 100;
npcList[npcGameObject].CurrentHealth = npcList[npcId].maxHealth;
npcList[npcGameObject].expReward = 50;
}
public class NPC {
public int currentHealth {get; set;}
public int maxHealth {get; set;}
public int expReward {get; set;}
}
}
Such solution provides you both game object and npc object in one single place, you can access npc object easly by giving gameObject instance.
I don't think I'm understanding this "Dictionary" Thing, If I was to create a GameObject with this script, and click on it... (Which I have a script that sets GameObjects to the target when clicked on) how would I go about loading THAT GameObjects information and not a randoms, that is why I wanted to get the NPCID stored, so I knew which NPC Did what. When I click on the target, it would get the ID and then send it to the method where it retrieves the stats.
If you click on game object, which i belive u realized with raycasting, you will get that game object, from RaycastHit (raycastHit.transform.gameObject). Then u can just assign that clicked game object, and use it as index for Dictionary, just like with array. Question is, if you have already created GameObject, or NPCs#createNPC() method should also create game object for that npc. If you have already existing game object, which you didnt mentioned about, then it would have to look different.
Assets/Scripts/Units/NPCs.cs(5,12): error CS0246: The type or namespace name Dictionary
2' could not be found. Are you missing a using directive or an assembly reference?
Hrm...
Ok, lets clear up something. Do you have GameObject of NPC already created, and THEN you want to run NPCs.createNPC(), and this method should create instance of NPC and bind it to specific game object ? What you want is:
foreach (GameObject npcGameObject in allNpcsGameObjects) {
NPC npc = new NPC();
npc.maxHealth = 100;
npc.CurrentHealth = npcList[npcId].maxHealth;
npc.expReward = 50;
npcGameObject.npcScript = npc;
}
is that right ?
Your answer
Follow this Question
Related Questions
Array of GameObjects - JS 1 Answer
C# SetActive GameObject Array 2 Answers
A node in a childnode? 1 Answer