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 /
  • Help Room /
avatar image
0
Question by codedbythet · Aug 05, 2021 at 12:17 AM · c#collisionprefabitem pickup

2D Shooter Make Item Pickup Change Bullet Prefab On Collision

I have a bullet script where the rigidbody(rb) is a prefab of one type of bullet, say earth and upon collision of another bullet type say fire, the prefab changes to fire. I have setup tags for each bullet but

 if (collision.gameObject.tag == "RedBullet")
 {
     PlayerShoot.Prefab "Then Item changes the Bullet"  == "RedBullet";
 }
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 rage_co · Aug 06, 2021 at 06:06 AM 0
Share

I don't know how your bullet system is setup but instead of doing this, you need to create all the different type of bullets as children of the player and set the desired bullet as the active ine when required, and create a database within the script to manage the different bullets and swap them out whenever you want.....an array is really good for this

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by codedbythet · Aug 07, 2021 at 02:57 AM

how do you tell the array to make the bullet active?

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 rage_co · Aug 07, 2021 at 09:14 AM 0
Share

Just run a simple for loop with a simple if tag which checks if the index of the current bullet matches the desired bullet, if yes, set it active and set it as the current bullet, else disable it with the SetActive statements...let me give you an example on how it can work, (you would have to modify it to fit your code ofcourse, but shouldn't be much)

 using UnityEngine;
 using System;
 
 public class BulletSwitch: MonoBehaviour
 {
   public GameObject[] bullets;
   public GameObject currentBullet;
   public int currentBulletIndex;
   public PlayerShoot playerShoot;
 
   void Update()
   {
     if(Input.GetKeyDown(Keycode.Q))
     {
       currentBulletIndex ++;
       if(currentBulletIndex >= bullets.Length)
       {
         currentBulletIndex = 0;
       }
       SetBullet();
     }
     else if(Input.GetKeyDown(KeyCode.E))
     {
       currentBulletIndex --;
       if(currentBulletIndex < 0)
       {
         currentBulletIndex = bullets.Length - 1;
       }
       SetBullet();
     }
   }
 
   public void SetBullet()
   {
     currentBullet = bullets[currentBulletIndex];
     playerShoot.bullet = currentBullet;
     for(int i=0; i < bullets.Length; i++)
     {
       if(i == currentBulletIndex)
       {
         bullets[i].SetActive(true);
       }
       else
       {
         bullets[i].SetActive(false);
       }
     }
   }  
 }


Change the name of PlayerShoot to whatever class your PlayerShoot script is of and change the name of playerShoot.bullet to whatever the name of the bullet is in your script....and then assign this script to the player in inspector....then make all the bullet prefabs children of the player....and assign them to the bullets array in the inspector...This should do what you intend.....hope this helps

avatar image codedbythet · Aug 17, 2021 at 04:45 PM 0
Share

This has been very helpful. So I am trying to build a c# that attaches to the missile that when the collider is activated the player shoot script bullet prefab object changes.

 void OnCollisionEnter2D(Collision2D collision) {
     if (collision.gameObject.CompareTag("Player")) {

I tried your script and I can get it to recognize the collision, but it instead of selecting the new bullet prefab, it only changes to the current one.

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

217 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 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 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

Cant figure out how to make a pickup that swaps one game object for another. 0 Answers

Player trigger on platform with prefab 0 Answers

How do I make item pickup change sprite on collision? 1 Answer

Picking up item if player is within a certain radius of the item. 0 Answers

why isnt my code working, 2 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