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 LPGaming-Company · Jan 23, 2013 at 01:43 PM · gameobjectarrayconstructor

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.

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

1 Reply

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

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.

Comment
Add comment · Show 7 · 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 LPGaming-Company · Jan 23, 2013 at 02:30 PM 0
Share

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.

avatar image ryba · Jan 23, 2013 at 02:54 PM 0
Share

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.

avatar image LPGaming-Company · Jan 23, 2013 at 03:18 PM 0
Share

Assets/Scripts/Units/NPCs.cs(5,12): error CS0246: The type or namespace name Dictionary2' could not be found. Are you missing a using directive or an assembly reference?

Hrm...

avatar image LPGaming-Company · Jan 23, 2013 at 03:21 PM 0
Share

never$$anonymous$$d, forgot to import generic

avatar image ryba · Jan 23, 2013 at 03:21 PM 0
Share

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 ?

Show more comments

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

10 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

Related Questions

Array of GameObjects - JS 1 Answer

C# SetActive GameObject Array 2 Answers

A node in a childnode? 1 Answer

Collecting game objects in specific order 1 Answer

C# Check If Gameobject is within Collider 1 Answer


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