Unity can’t get value from other script

Updated on March 22, 2017 in [A] C# .Net
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
2 on March 22, 2017

Hey guys so I have tried everything static variables, instances getComponent and I cannot get the value from my other script. In the other script it shows the correct value but in the other I am trying to get it from it is always 0 it should be 1.

So this is the script with the problem:

public class FireGun : MonoBehaviour {
// public List<GameObject> Guns = new List<GameObject>();
//  public PickupBx pbx;
private int count;
// Use this for initialization

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

void Update () {
Debug.Log(PickupBx.count + " this is weapon count");

//if weapon count is one then loop through contents in array in the pickupbox class and add them to the guns add here

//if(pbx.weaponCount == 1)


//    foreach(GameObject gun in pbx.guns)

//    {

//        Guns.Add(gun);

//    }



This is the main script where the value is correct:

using UnityEngine;

using System.Collections;

using System.Collections.Generic;
public class PickupBx : MonoBehaviour


//Spawn locations for boxes

public Vector2[] box_locations;
//this gets the prefab

public GameObject boxPrefab;
public GameObject player;
public GameObject[] gunPrefab;
//this stores the current gameobject which is in the game

public GameObject activatedGameObject;
//keep count of times weapon collected

public static int count = 0;
//Randomly generated number for weapons.

private int randomWeaponNumber;
//holds the randomNumber

private int random_number;
//holds the gun objects

public List<GameObject> guns = new List<GameObject>();
//checks if weapon is collected

private bool isWeaponCollected = false;
// Use this for initialization

void Start()


//Generate random number between 0-3 there are 4 vector2 locations for the box.

random_number = Random.Range(0, 3);
//Instantiate the prefab at a random location and set its rotation to default

activatedGameObject = (GameObject)Instantiate(boxPrefab, box_locations[random_number], Quaternion.identity);
//if tag on box object is not box then set it to box (already configured tag, this is here for extra security)

if (boxPrefab.tag != "box")

boxPrefab.tag = "box";

//keep generating random numbers through Update

void Update()


//this will store the game object that has been found

GameObject activeGunObject;
//this part will need fixing later

if (this.gameObject.GetComponent<SpriteRenderer>().flipX == true && GameObject.FindGameObjectWithTag("Gun") != null)


//activate game object is equal to the gun found in the game

activeGunObject = GameObject.FindGameObjectWithTag("Gun");

//flip gun to x axis

activeGunObject.GetComponent<SpriteRenderer>().flipX = true;

//this is the position of the gun when x axis is true

activeGunObject.transform.localPosition = new Vector2(-44.1f, 0);
else if (this.gameObject.GetComponent<SpriteRenderer>().flipX == false && GameObject.FindGameObjectWithTag("Gun") != null)


activeGunObject = GameObject.FindGameObjectWithTag("Gun");

activeGunObject.GetComponent<SpriteRenderer>().flipX = false;

activeGunObject.transform.localPosition = new Vector2(44.1f, 0);

//random Number for box location

random_number = Random.Range(0, 3);
//random Number for random gun

randomWeaponNumber = Random.Range(0, 3);
// guns in inventory/list greater than or equal to 2 delete the gun

if (guns.Count >= 2)


//we destroy the gun otherwise there will be multiple guns in the scene (reaons is guns.count - guns.count is because some weird glitch if we do guns.count -1 count enters twice

guns.RemoveAt(guns.Count - guns.Count);
//put back to zero

count = 0;
//is true because weapon collected

isWeaponCollected = true;

//Call the destroy weapon function


void OnTriggerEnter2D(Collider2D col)


if (col.gameObject.tag == "box")


//add one to count

count += 1;
//Debug.Log(count + "count");

//Debug.Log("weapon count" + weaponCount);

Debug.Log(count + "ORIGINAL SCRIPT");
//take box to random location

activatedGameObject.transform.position = box_locations[random_number];
//spawn random gun

GameObject currentGun = Instantiate(gunPrefab[randomWeaponNumber]);
//the transform of the player

Transform cameraTransform = player.transform;
//Makes the player the parent of the GameObject currentGun

currentGun.transform.SetParent(cameraTransform, false);
//add gun to list

count = 0;



public int weaponCount




return count;


//this weapon will destroy the weapon so there is one 1 in the scene

void DestroyWeapon()


if (isWeaponCollected == true)


//Destroy the gameobject with the gun tag so we don't make multiple guns

//set to false so update doesn't call this method.

isWeaponCollected = false;



  • Liked by
0 on March 22, 2017

Line 137

Debug.Log(count + "ORIGINAL SCRIPT"); in the second script is where I test it and the value is fine.
  • Liked by
0 on March 22, 2017

Doing some more research works fine with a normal variable I think its because count gets set to 0 really fast in the update function so it never changes?

  • Liked by