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 Morvar · Sep 30, 2013 at 07:10 PM · instantiateenemycreateitem

How should I create/instantiate a skill/enemy/item

I was wondering what's the correct way to create/instantiate skills, enemies and items with varying parameters.

In my case it would be creating a custom skill to be thrown on field based on player's stats/skills. The skill has variables like damageFrom, damageTo, duration, delay and sometimes there can be a custom variable.

My current game is based on instantiating where it instantiates an empty object with my "BaseSkill" script. Then I was searching for a way to set these mentioned parameters and I ran into a way suggesting something like this:

 skill1 = clonedPrefab.GetComponent<BaseSkill>().setDamage(1,3);
 skill1 = clonedPrefab.GetComponent<Basekill>().setDuration(3);
 ....


... which looks a bit heavy, especially if there can be dozens of this same action happening at the same time. Should I go with objects as I'm working with C#, or something else? An example or link to a better way would be appreciated. Haven't done objects with C# before

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 robertbu · Sep 30, 2013 at 07:29 PM

You want to create a reference and then set them via that reference. You don't want to call GetComponent() for each one. Plus your syntax is wrong here (i.e. it would not compile).

 BaseSkill skill1 = clonedPrefab.GetComponent< BaseSkill >();
 skill1.SetDamage(1,3);
 skill1.SetDuration(3);

Note by convention Methods and Classes start with a upper case letter. Variables start with a lower case letter. Nothing enforces this convention, but folks on the list expect it and therefore find code not written to this convention harder to read.

Comment
Add comment · Show 2 · 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 Morvar · Sep 30, 2013 at 07:44 PM 0
Share

Thanks Robertbu. The code was just for demonstrating purposes anyway and it's not actually written anywhere.

So instantiating is the best way to make skills/enemies/items or was this just a suggestion to make my way work?

avatar image robertbu · Sep 30, 2013 at 08:32 PM 0
Share

I've have to understand more about your skills and their relationship to the character to make a recommendation, and even then I'm not the expert. As a guess based on this limited information, I would suggest building your skills out of classes not derived from $$anonymous$$onobehaviour. These would be "standard" classes that would not require a game object to exist. In other words, they would not be components. You would create them with a 'new' operator and assign them to a parameter in your character or object.

And then there is the whole question of how you handle conceptually different skill sets. In the limited number of times I've had this issue, I've created a base class and derived the individuals from that base class. But there is a very interesting answer by @whydoidoit that suggest other approaches might be better:

http://answers.unity3d.com/questions/545157/how-to-store-a-javascript-in-a-variable-in-the-edi.html

I don't know if this is an issue for your skills.

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

14 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

Related Questions

Enemy instantiate script, vars not showing in Inspector and error I can't fix 1 Answer

Item Drop Help 1 Answer

copy enemy prefabs 1 Answer

How to remove all children objects & how to instantiate prefab as child to specific object 5 Answers

The problem with FindObjectOfType and Instantiate.Why is not one object created, but a multiplied copy? 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