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
0
Question by LPGaming-Company · Jan 23, 2013 at 04:35 PM · dictionarynpctargettinguntiy

Need help with targetting

So, I have a basic Raycast targetting system set up...

        if (Input.GetButton("Fire1"))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast (ray, out hit, 250))
             {
                 if (hit.transform.tag != "terrain")
                 {
                     target = hit.transform;
                     Debug.Log ("Target Tag = " +hit.transform.tag);
                 }
                 } else {
                     target = null;
                 }
             }


but, I need to find a way to incorperate it with the following code, so I can get the stats of my target. I was told that there is a way to pull the selected npc from the dictionary, please note that I am not using MonoBehavior scripts so it's not as easy as drag/drop. I would think that I'd have to set up some sort of Identification system, such as "public in NPCID" and try to figure out how to incorperate that, although I'm not sure...

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class NPCs {
     public static NPCs instance;
     
     public static NPCs GetInstance()
     {
         if (instance == null)
         {
             instance = new NPCs();
         }
         return instance;
     }
     
     public Dictionary<GameObject, NPC> npcList = new Dictionary<GameObject, NPC>();
 
     public void createNPC(string name, int maxHealth, int expReward, int maxDamage, Vector3 position) {
        // Instantiate game object here
         GameObject npcGameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
         NPC npc = new NPC();
         npcList.Add(npcGameObject, npc);
         npcGameObject.transform.position = position;
         npcGameObject.tag = "Mob";
         npcList[npcGameObject].npcName = name;
         npcList[npcGameObject].maxHealth = maxHealth;
         npcList[npcGameObject].currentHealth = npcList[npcGameObject].maxHealth;
         npcList[npcGameObject].expReward = expReward;
         npcList[npcGameObject].npcDamage = maxDamage;
     }
 
     public class NPC {
         public string npcName {get; set;}
          public int currentHealth {get; set;}
         public int maxHealth {get; set;}
          public int expReward {get; set;}
         public int npcDamage {get; set;}    
     }
 }
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
0
Best Answer

Answer by LPGaming-Company · Jan 23, 2013 at 04:50 PM

If anybody else is doing something like this or wants to use this as a reference while they are googling their problems in the future, here is the solution.

You don't do anything to the targetting.

You just call it like this.

 NPCs.GetInstance().npcList[target.gameObject].(Whatever)
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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a GameObject as the key in a generic dictionary - performance issues? 0 Answers

What's the best way to use prefabs as Dictionary values? 2 Answers

Getting the sum of random values in the dictionary 2 Answers

unity dictionary with array as keys causes problems 0 Answers

Variable target in 'Targetting system' error. 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