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 HappyGuyDK · Aug 28, 2014 at 01:09 PM · c#shootingbulletaddcomponent

AddComponent adds too many (C#)

My move script currently adds a bullet and adds the script BulletScript to the bullet when the user presses X.

The problem here is that it adds too many BulletScripts to my bullet game object. Like it's in an infinite loop or something.


Here's the shoot part of my move script

 void Update () {
     if(Input.GetKeyDown("x") &&  Time.time > NextFire) {
         NextFire = Time.time + FireRate;
         shoot.gameObject.AddComponent<BulletScript>();
         shoot = Instantiate(shoot) as GameObject;
     }
 }


And here's my bullet script

 private GameObject player;

 void Start () {
     player = GameObject.Find ("Player");
     gameObject.AddComponent<Rigidbody>();
     transform.position = new Vector3(player.transform.position.x,player.transform.position.y,player.transform.position.z);
     collider.isTrigger = true;
     rigidbody.isKinematic = true;
     rigidbody.freezeRotation = true;
 }
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 RedDevil · Aug 28, 2014 at 01:32 PM 0
Share

Wouldn't it just be easyer to make a prefab of the bullet with the script already atached and just instantiate the bullet?

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by HappyGuyDK · Aug 28, 2014 at 02:34 PM

Thanks guys but I figured it out!

It was because stupid me had made a script that added a lot to the bullet OBJECT so the object already had a million scripts attached to it.

Also: I know my code is kinda werid right now but don't worry! I'll get back to it and fix it!

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 HappyGuyDK · Aug 29, 2014 at 08:04 AM 0
Share

Correction! It's called a prefab and not an object. (Just to avoid confusion)

avatar image
0

Answer by Sisso · Aug 28, 2014 at 01:11 PM

Double look your code:

 shoot.gameObject.AddComponent<BulletScript>();
 shoot = Instantiate(shoot) as GameObject;

First you add a new bullet component into a already existent shot, then you clone it. Got it?

So, each fire you add a new BulletScript and then clone it (2 scripts). Next fire, add a new one and clone (3 scripts) and so on.

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
avatar image
0

Answer by AyAMrau · Aug 28, 2014 at 01:12 PM

what's the shoot variable? you seem to use it both as the prefab and then the bullet object, which probably causes it to accumulate all those components.

Also why not just have the component on the prefab?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroying Object when Adding Component 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to Sync bullet spawn with shoot animation 0 Answers

sending message to bullet (javascript) 1 Answer

How do I make the gun stop shooting when I am out of ammo? 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