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 eamonb · Mar 01, 2015 at 06:53 PM · variableinheritancenewinstantiation

Cannot use "new" keyword on class inheriting from MonoBehaviour

I have a class called ShootBulletPlayer.cs and in it I have a Gun class (which doesn't inherit from MonoBehaviour), and two types of guns that inherit from it: StandardGun and PushBackGun.

 public class Gun {
     public string prefabLocation;
     public GameObject gun;
     public Bullet bullet;
     public Gun()
     {
     }
 }
 
 public class StandardGun : Gun
 {
     public StandardGun()
     {
         prefabLocation = "Prefabs/RayGun";
         bullet = new Bullet.StandardBullet();
     }
 }
 
 public class PushBackGun : Gun
 {
     public PushBackGun()
     {
         prefabLocation = "Prefabs/RayGunV2";
         bullet = new Bullet.PushBackBullet();
     }
 }


As you can see, each gun has its own special bullet it's referencing upon construction which has its own unique properties. I get the following error when I create each bullet in each specific guns constructor: "You are trying to create a MonoBehaviour using the 'new' keyword. MonoBehaviours can only use added using AddComponent()" I know this is because my Bullet class inherits from MonoBehavior and therefore I should not use the "new" keyword to create a new instance of each bullet, but I don't know any other way to assign a new bullet to each gun upon its construction. Here is my bullet code which is located in a class called Bullet.cs:

 public class Bullet : MonoBehaviour{
 
     public GameObject bulletObject;
     private float pushBackAmount;
     public float shotSpeed;
     public string prefabLocation;
     public float spawnDistanceFromGun;
     
 
     public class StandardBullet : Bullet
     {
         public StandardBullet()
         {
             pushBackAmount = 2f;
             shotSpeed = 100f;
             prefabLocation = "Prefabs/StandardBullet";
             spawnDistanceFromGun = 2f;
         }
     }
     
     public class PushBackBullet : Bullet
     {
         public PushBackBullet()
         {
             pushBackAmount = 5f;
             shotSpeed = 100f;
             prefabLocation = "Prefabs/PushBackBullet";
             spawnDistanceFromGun = 2f;
         }
     }

} As you can see, each bullet has its own properties like "pushBackAmount" and "shotSpeed." How can I create a new bullet in each gun's constructor so that the variables like "pushBackAmount" and "shotSpeed" will be correctly passed?

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
0

Answer by gjf · Mar 01, 2015 at 08:55 AM

if you're inheriting from MonoBehaviour, your script is a component so add it as one...

and put the inherited classes in their own script.

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 eamonb · Mar 01, 2015 at 07:11 PM 0
Share

I understand that but I don't know the correct coding syntax. I'm trying but I can't get it to compile. That is why I posted my code and the dependencies so that someone could hopefully help provide me the correct syntax to create a new bullet in the gun constructor

avatar image
0

Answer by siaran · Mar 01, 2015 at 08:51 PM

Don't create a new bullet in the gun constructor - that doesn't even make sense.

Create prefabs for all your types of bullets and set their values, then drag the correct one into the bulletObject field on your Gun script in the editor for each type of gun.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Store Multiple values in one variable 2 Answers

Create a new script object from a class which inherits from MonoBehavior 4 Answers

Trying to call a variable from another script in C# 1 Answer

Small Inheriting Problem 1 Answer

can't change variable through other script 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