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 RuggeriExtreme · Oct 28, 2013 at 09:40 PM · itemweapon systemclass objectmono-behaviour

Item-Weapon Class

Hi everybody, i have written these classes:

 Item : Monobehaviour

 Abstract Weapon : Item
 
 Gun : Weapon
 //Methods that use Coroutine for fire, recoil and others..

 public void RechargeGun()
     {
         StartCoroutine(Recharge());
     }
     
     private IEnumerator Recharge()
     {
         yield return new WaitForSeconds(RechargeTime);
     }


This approach is correct?

If i try to instantiate a instance of Gun or Item and i initialize it, for example:

 public Gun myGun = new Gun();

When i start the game, a message appear in the console: You are trying to create a Monobehaviour using the 'new' keyboard. This is not allowed. Monobehaviour can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all.

It's not error but this message alert me..

Thanks at all, i need your help.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jonnyhopper · Oct 28, 2013 at 09:49 PM

Yes, you cannot create anything that inherits from a Monobehaviour using new()

Instead you need to make a new game object and attach the component to it, for example:

 GameObject myGunObject = new GameObject( "Gun" )
 myGunObject.AddComponent< Gun >();

then you can do

 Gun gun = myGunObject.GetComponent< Gun >();
 gun.RechargeGun();
Comment
Add comment · Show 1 · 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 Random10000000000000000000000 · Apr 20, 2015 at 04:04 PM 0
Share

How would I add a graphical object to the gunObject as well?

avatar image
0

Answer by RuggeriExtreme · Oct 28, 2013 at 09:54 PM

Ah, so i need a gameobject for initalize a new istance of Gun..

If I remove Item: Monobehaviour and i don't use the coroutine, the result is better?

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 jonnyhopper · Oct 28, 2013 at 09:56 PM 0
Share

I guess so but it really depends what you want to use it for. Do you want the gun to be attached to a character and so on, have a position and an update function etc etc?

avatar image RuggeriExtreme · Oct 28, 2013 at 10:06 PM 0
Share

In reality it is a very abstract concept. I have a lot of different type of weapons such as guns, spears, arcs and others, for each i need a method, for example Fire or Recharge, in the Weapon Class abstract i define these methods and in the subclass such as Gun, Arcs, Spear i implement them with a different function in relation with the weapon type..

Thanks for the reply

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

16 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

Related Questions

`DecalMode' could not be found 1 Answer

Reflect changes of script in editor 2 Answers

Invoking Effects of (Random) Items (C#) 0 Answers

Show GUITexture by picking an item. 0 Answers

How to add a video intro on the start game? 3 Answers


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