• 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 $$anonymous$$ · Mar 08, 2018 at 08:52 PM · gameobjectshootingspace

I'm Trying To Make A Power Up System For The Space Shooter Tutorial Project

I Have A GameObject With My Player With One Gun And The Player With 3 Guns I Have A Little Power Up Orb That I Want To Make This Happen My Only Issue Is I Am Having Troubles Swapping Between The 2 Characters And They Stay At The Same Position

Here Are My Scripts:

[CODE=CSharp] using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events;

[Serializable]

public class PowerUp {

 [SerializeField]

 public string name;

 [SerializeField]

 public float duration;

 [SerializeField]

 public UnityEvent startAction;

 [SerializeField]

 public UnityEvent endAction;

 public void End() {

     if (endAction != null) {

         endAction.Invoke();
         
     }

 }

 public void Start() {

     if(startAction != null){

         startAction.Invoke();

     }

 }

} [/CODE]

[CODE=CSharp] using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PowerUpController : MonoBehaviour {

 public GameObject powerUpPrefab;
 public List<PowerUp> powerUps;
 public Dictionary<PowerUp, float> activePowerUps = new Dictionary<PowerUp, float>();

 private List<PowerUp> keys = new List<PowerUp>();
 

 // Update is called once per frame
 void Update () {

     HandleActivePowerUps();

     if (Input.GetKeyDown(KeyCode.LeftShift)) {

         SpawnPowerUp(powerUps[Random.Range(0, powerUps.Count - 1)], new Vector3(3.0f, 0.0f, -3.0f));

     }

 }

 public void HandleActivePowerUps() {

     bool changed = false;

     if (activePowerUps.Count > 0) {

         foreach (PowerUp powerUp in keys) {

             if (activePowerUps[powerUp] > 0)
             {

                 activePowerUps[powerUp] -= Time.deltaTime;

             }

             else {

                 changed = true;

                 activePowerUps.Remove(powerUp);

                 powerUp.End();

             }

         }

     }

     if (changed == true) {

         keys = new List<PowerUp>(activePowerUps.Keys);

     }

 }

 public void ActivatePowerUp(PowerUp powerUp) {

     if (!activePowerUps.ContainsKey(powerUp))
     {

         powerUp.Start();
         activePowerUps.Add(powerUp, powerUp.duration);

     }

     else {

         activePowerUps[powerUp] += powerUp.duration;
         keys = new List<PowerUp>(activePowerUps.Keys);

     }

 }

 public GameObject SpawnPowerUp(PowerUp powerUp, Vector3 position) {

     GameObject powerUpGameObject = Instantiate(powerUpPrefab);

     var powerUpBehaviour = powerUpGameObject.GetComponent<PowerUpBehaviour>();

     powerUpBehaviour.controller = this;

     powerUpBehaviour.SetPowerUp(powerUp);

     powerUpGameObject.transform.position = position;

     return powerUpGameObject;

 }

}

[/CODE]

[CODE=CSharp] using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PowerUpBehaviour : MonoBehaviour {

 public PowerUpController controller;

 [SerializeField]

 private PowerUp powerUp;
 private Transform tf;

 private void Awake() {

     tf = GetComponent<Transform>();

 }

 private void OnTriggerEnter(Collider other) {

     if (other.gameObject.tag == "Player") {

         ActivatePowerUp();
         gameObject.SetActive(false);

     }

 }

 private void ActivatePowerUp() {

     controller.ActivatePowerUp(powerUp);

 }

 public void SetPowerUp(PowerUp powerUp) {

     this.powerUp = powerUp;
     gameObject.name = powerUp.name;

 }

}

[/CODE]

[CODE=CSharp] using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PowerUpActions : MonoBehaviour {

 [SerializeField]
 
 public GameController gameController;
 public GameObject player;
 public GameObject playerUpgradedWeapons;

 private PlayerController playerController;
 private PowerUp powerUp;

 public void TripleWeaponStartAction() {
     
     //Need To Swap Between The Player Which Only Has One Gun To The Player With 3 Weapons For The Duration Of The Power Up

     player = GetComponent<GameObject>();
     playerUpgradedWeapons = GetComponent<GameObject>();

     if (player) {

         //To Turn Off
         gameObject.SetActive(false);

     }
     if (playerUpgradedWeapons) {

         //To Turn On
         gameObject.SetActive(true);

     }

     TripleWeaponEndAction();

 }

 public void TripleWeaponEndAction()
 {

    

 }

}

[/CODE]

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 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

Related Questions

Are there any pitfalls for putting space in GameObject/Prefab names? 1 Answer

How to change X and Y axis of the gameObject while moving? 0 Answers

SetActive between gameobjects 1 Answer

Another guy who doesnt get the Destroy function to work. 1 Answer

Speed up Gameobjects Based on Score 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