Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 MarkusKarlsson · Jun 23, 2016 at 05:15 PM · scripting problemgameobjectgetcomponentnpcfighting

Passing enemy statistic to FightController

I created a fighting system between player and enemy with easy customization so I can just copy/paste the old enemy and change some values like speed and health to create completely new enemy. I build this without knowing I can't have a script with the same name in many different folders. My system work so that when collision between player and enemy is detected, InitFight()-function is called (in my FightHandler). This InitFight()-function searches the enemy gameObject collided with for a script called "Stats" and gets enemy variables from there like this:

 enemyDefence =  GV.NPCUnderAttack.GetComponent<Stats>().defence; 

I spent nearly 30 hours making my FightHandler what I think is perfect for my needs but when I started creating my first new enemy I got this error: "The namespace `global::' already contains a definition for" which means I have two scripts with the same name in different locations. To make it more understandable look at demonstration of my project hierarchy (also the Chicken folder has ChickhenColor, ScoutAround and Stats): alt text

I've tons of code in my Fighting system and it all relies enemies having a script called Stats otherwise nothing will happen. I clearly wouldn't like the start planning everything from the beginning so does anybody got good ideas? Could I use something like:

 void OnCollisionTrigger(Collider col){
     string nameOfNPC = col.gameObject.name;
     string scriptToSearchFor = nameOfNPC + "Stats";

     enemyDefence =  GV.NPCUnderAttack.GetComponent<scriptToSearchFor>().defence; 
 }

And then just name each of the enemies Stats.cs file like: CowStats.cs, ChichkenStats.cs, ReindeerStats.cs. Would that be a good way? If it's possible how would it affect my performance?

Thanks in advance

uniyq.png (23.2 kB)
Comment
Add comment · Show 1
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 Jessespike · Jun 23, 2016 at 05:22 PM 0
Share

You have multiple scripts named Stats? Why not have one script that gets used by all enemies? Then it can stay simple:

  void OnCollisionTrigger(Collider col){
      enemyDefence =  col.gameObject.GetComponent<Stats>().defence; 
  }

0 Replies

· Add your reply
  • Sort: 

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

75 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

Related Questions

Changing a Prefab's Text component seems to be broken. 1 Answer

GetComponent returning null inside script, but working well outside 0 Answers

Destroyed instance of Prefab, can't spawn it back. 1 Answer

I have a character object type that I want to make into a game object 0 Answers

Game Objects not using same script independently. 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