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!
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;
}
Your answer
Follow this Question
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