Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Onx · Jul 17, 2020 at 08:40 AM · powerup

Help with code

Hello,

Sorry, not sure where or how to post this correctly but here goes,

I have 2 different scripts on two different objects, a player controller and a powerup with its own script. The power up script needs to update a float value in the player controller (jumpEnergy) when the power up is collected.

Right now it works, but every time I drop a power up into the level I have to define the "Player" in the inspector by pointing to my Player entity in the hierarchy. This makes level design time consuming and will probably not work if they are spawned during gameplay.

I'm pretty sure that I can have the Player targeted in the script so I don't have to find it each time I add the powerup to a map, Can anyone help me achieve this please?

And Thank you.

The two scripts are below: playerCntroler.cs

 public class playerController : MonoBehaviour {
 
     public float speed;
 
     public float jumpEnergy; //jump enery amount at any time
     public float jumpForce; //energy used per jump
 //    public float distanceToGround;
     private Rigidbody rb;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody> ();
     }
 
     // Update is called once per frame
     void Update ()
     {
         PlayerMovement();
         if (Input.GetKeyDown (KeyCode.Space) && (jumpEnergy > 1f))
         {
             PlayerJump ();
         }
     }
         
     void PlayerMovement()
     {
         float hor = Input.GetAxis ("Horizontal");
         float ver = Input.GetAxis ("Vertical");
         Vector3 playerMovement = new Vector3(hor, 0f, ver) * speed * Time.deltaTime;
         transform.Translate(playerMovement, Space.Self);
     }
 
     void PlayerJump()
     {
         rb.AddForce (Vector3.up * jumpForce, ForceMode.Impulse);
         jumpEnergy = (jumpEnergy - jumpForce);
     }
 
 }
 

And the powerup code pickUpJumpEnergy.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class pickUpJumpEnergy : MonoBehaviour
 
 {
     public playerController player;
     public static float pickupsize = 50f;
     //public AudioSource collectSound;
  
     void Awake()
     {
         player = player.GetComponent <playerController>();
     }
 
     void ApplyPowerUp ()
     {
         print ("Item Picked Up");
         Destroy (gameObject);
         player.jumpEnergy = (player.jumpEnergy + pickupsize);
     }
 
     void OnTriggerEnter(Collider collider)
     {
         if (collider.gameObject.tag == "Player")    
         {
     //      collectSound.Play ();
         ApplyPowerUp();
         }
     }
 }
 


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 Nydergond · Jul 17, 2020 at 09:10 AM

You can use the player has a singleton.

Singletons are great for SinglePlayer games that have only one instance of something in the game or scene.

To make a singleton pattern just add to your player class the following:

  public static PlayerController instance;

  public float speed;
 
  public float jumpEnergy; //jump enery amount at any time
  public float jumpForce; //energy used per jump
  private Rigidbody rb;

  private void Awake(){
        if (instance!= null && instance!= this)
        {
             Destroy(this.gameObject);
        } else {
            instance= this;
        }  
  }
  ......


Then in your powerup script you can call the player script and replace the line of code you do the logic with that:

  void ApplyPowerUp ()
  {
      print ("Item Picked Up");
      Destroy (gameObject);
      instance.jumpEnergy = (instance.jumpEnergy + pickupsize);
  }
 
  void OnTriggerEnter(Collider collider)
  {
      if (collider.gameObject.tag == "Player")    
      {
  //      collectSound.Play ();
      ApplyPowerUp();
      }
  }

To get more info you can watch this tutorial or look for other information reference:

https://www.youtube.com/watch?v=EI1KJv8owCg

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

130 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

Related Questions

How to make a loop for PowerUps/Bonus 2 Answers

Audio and Particles on Trigger 0 Answers

Input Problems 2 Answers

Powerup Issues (Heart Point Addition and Infinite Double Point Can't Stop When Get Another Powerup While Having Powerup That Doesn't Stop Yet) 1 Answer

magnetic Powerup Timer Problem 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