Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 vcstylez1 · Nov 15, 2017 at 04:18 PM · variablesreferencingaccessing

Accessing a variable from another script (Bomberman Bomb Limit)

Hi, sorry for repeating this question for the millionth time however after going through all the other previous posts on this question and trying their answers I still have no joy. After 6 hours of trying to figure this out I am at wits end and so request help from you geniuses out there haha. I am basically trying to create a bomb limit (i.e. 3) that will decrease when a bomb is placed and increase when it explodes.

I have 2 scripts, Player and Bomb.
Player Script:

 public class Player : MonoBehaviour {
 
     //These are the variables I am trying to access from my Bomb Script
     public int p1bombs = 3;
     public int p2bombs = 3;
 
         if (canDropBombs && p1bombs > 0 && Input.GetKeyDown(KeyCode.Space)) {
             DropBomb();
             p1bombs--; // decrease value when DropBomb is called by player 1
         }

Bomb Script:

     public class Bomb : MonoBehaviour {
   void Explode() {
     public GameObject playerGameObject;
             Player playerScript = playerGameObject.GetComponent<Player>();
             playerScript.p1bombs = 3; //after explode, reset value to 3
             playerScript.p2bombs = 3;
 }

With these snippets of code, I have no compiler errors (tbh ive tried doing this so many different ways, I don't know up from down anymore) but after the bombs explode, it doesn't reset the value back to 3 in my Player Script.

Also, what I wanted to do first was to use private access modifiers in my Player script for p1bombs and p2bombs, however I was unable to figure out how to access a private variable from another script or whether that was even the right way of going about this, opinions and help?

P.s. I have tried to post only the code thats relevant to my question, if more is necessary then let me know what :) thanks in advance!

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 KittenSnipes · Dec 12, 2017 at 06:16 PM

@vcstylez1 Sounds like you need a Dictionary. No this is not a joke. This thing is called a Dictionary. What a Dictionary does is hold a value at an index you specify.

Here is an example code based on what you want:

 Dictionary<string, int> bombsPerPlayer;
 public int bombsAllowedPerPlayer = 3;
 void Start() {
     //Create the dictionary
     bombsPerPlayer = new Dictionary<string, int>();
 
     //This adds the bombsAllowedPerPlayer to that specific players name.
     bombsPerPlayer.Add(transform.name, bombsAllowedPerPlayer );
 }
 
 void Update() {
     //If the player has pressed space and has more than 0 bombs
     if (Input.GetButtonDown("Jump") && bombsPerPlayer[transform.name] > 0) {
         //Then toss that bomb or whatever.

         //If a bomb is tossed then subtract the amount of bombs the player has
         bombsPerPlayer[transform.name] -= 1;
     }
 }
 
 void Explode() {
     //This is how to access that players allowed amount of bombs to throw
     //Since the bomb has exploded we add 1 more to the amount they are allowed to throw
     bombsPerPlayer[transform.name] += 1;
     
 }
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

119 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

Related Questions

How to access a variable from another C# script in Unity 1 Answer

Access variables or methods from another class (or gameObject) [C#] 1 Answer

How to reference to a local variable from a struct 0 Answers

Reference to InputField containing the script 0 Answers

How to track variables by reference.. 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