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
1
Question by SalDaDoor · May 28, 2021 at 07:17 PM · 2d gameinstantiate prefabweapon systempick up

Replacing Weapon 2D Shooter Unity

I am working on a system where you can go up to walls and buy weapons off the wall, similar to CoD Zombies. I can't find anything on picking up weapons in 2D however. I've come up with a system however and it needs some finishing touches. alt text

This is how I've structured my weapon system since I want the player to have a max of two weapons. I just need a way when the player buys a new gun for that prefab to instantiate as child of primary or secondary, depending on which is active, and to destroy the existing child.

Here's the code I've come up with so far, NOTE it currently can only instantiate as a child of the primary with this format. Any help please. For me and the community!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponBuy : MonoBehaviour
 {
     public GameObject OpenPanel = null;
 
     public WeaponSwitch weapon_switch;
 
     public GameObject weapon1;
     public GameObject parentObject;
 
     public Transform Spawnpoint;
 
     void Start ()
     {
       
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
         {
             OpenPanel.SetActive(true);
         }
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         OpenPanel.SetActive(false);
     }
 
     private bool IsOpenPanelActive
     {
         get
         {
             return OpenPanel.activeInHierarchy;
         }
     }
 
     void Update()
     {
         if (IsOpenPanelActive)
         {
             if (Input.GetKeyDown(KeyCode.F))
             {
                 OpenPanel.SetActive(false);
                 Debug.Log("Weapon Bought");
                 GameObject childObject = Instantiate (weapon1, Spawnpoint.position, Spawnpoint.rotation) as GameObject;
                 childObject.transform.parent = parentObject.transform;
             }
         }
     }
 }


unity-help.png (4.1 kB)
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

Answer by SalDaDoor · May 30, 2021 at 05:28 PM

The Code that I wrote that now works as intended. Only I am trying to deduct points, which the script is, but if the player doesn't have enough points it will simply deduct the points into the negative and not give the player a weapon. Any tips on this?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponBuy : MonoBehaviour
 {
     public GameObject weapon1;
     public GameObject parentPrimary;
     public GameObject parentSecondary;
 
     public GameObject primary;
     public GameObject secondary;
 
     public GameObject OpenPanel = null;
 
     public int amountToPurchase;
 
     public Transform Spawnpoint;
 
     public GameObject player;
 
     public float pointsToTake;
 
     private float points;
 
     void Start()
     {
         points = player.GetComponent<TopDownMovement>().points;
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
         {
             OpenPanel.SetActive(true);
         }
     }
 
     void OnTriggerExit2D(Collider2D other)
     {
         OpenPanel.SetActive(false);
     }
 
     private bool IsOpenPanelActive
     {
         get
         {
             return OpenPanel.activeInHierarchy;
         }
     }
 
     void Update()
     {
         if (IsOpenPanelActive)
         {
             if (Input.GetKeyDown(KeyCode.F))
             {
                 OpenPanel.SetActive(false);
                 player.GetComponent<TopDownMovement>().points -= pointsToTake;
                 Debug.Log("Weapon Bought");
                 if(points >= amountToPurchase)
                 {
                     if (primary.activeSelf)
                     {
                         Destroy(primary.transform.GetChild(0).gameObject);
                         GameObject childObject = Instantiate(weapon1, Spawnpoint.position, Spawnpoint.rotation) as GameObject;
                         childObject.transform.parent = parentPrimary.transform;
                     }
                     if (secondary.activeSelf)
                     {
                         Destroy(secondary.transform.GetChild(0).gameObject);
                         GameObject childObject = Instantiate(weapon1, Spawnpoint.position, Spawnpoint.rotation) as GameObject;
                         childObject.transform.parent = parentSecondary.transform;
                     }
                 }
                 else
                 {
                     Debug.Log("Not Enough Points");
                 }
             }
         }
     }
 }
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

158 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

Related Questions

Switching Weapons with Weapon Wheel 1 Answer

How would one go about making an asteroid field in a circle around an empty object in 2D? 0 Answers

How to decide an object's position when using Instantiate? 1 Answer

Spawning 2 bullets when I want 1 2 Answers

Endless level generation not instantiading 0 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