Referencing non-static variables
I'm making a basic basket ball game and i have a public boolean called dunkComplete which activated after the ball is dunked and is attaced to the ball script, i am trying to reference this boolean in a game manager script but for some reason even though dunkComplete becomes true its game manager counterpart doesn't, heres the game manager script for reference.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class game_manager : MonoBehaviour {
public GameObject Basket;
private float x_value;
private float y_value;
public GameObject ball;
private ball_script basketBallScript;
private bool dunkCompleteOperation;
// Use this for initialization
void Start () {
basketBallScript = ball.GetComponent<ball_script>();
Vector2 randomVector = new Vector2(Random.Range(-9f, 9f), Random.Range(0f, 3f));
Debug.Log(randomVector);
Instantiate(Basket, randomVector, transform.rotation);
Instantiate(ball, new Vector2(0, -3.5f), transform.rotation);
}
// Update is called once per frame
void Update () {
dunkCompleteOperation = basketBallScript.dunkComplete;
if (dunkCompleteOperation == true)
{
Vector2 randomVector = new Vector2(Random.Range(-9f, 9f), Random.Range(0f, 3f));
Instantiate(Basket, randomVector, transform.rotation);
}
}
}
Any help would be greatly apreciated thanks.
Comment
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ball_script : $$anonymous$$onoBehaviour {
public Rigidbody2D rb;
public float force = 5f;
public float torque = 0.1f;
private Vector2 startSwipe;
private Vector2 endSwipe;
private float x_swipe;
public float dunkTime;
private float tempDunkTime;
private bool dunked;
public bool dunkComplete;
// Use this for initialization
void Start ()
{
rb.velocity = new Vector2(0, 0);
dunkComplete = false;
dunked = false;
rb.is$$anonymous$$inematic = true;
tempDunkTime = dunkTime;
}
// Update is called once per frame
void Update ()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
startSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
}
if (Input.Get$$anonymous$$ouseButtonUp(0))
{
endSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
Swipe();
}
if (dunked == true)
{
tempDunkTime -= Time.deltaTime;
}
if (0 > tempDunkTime)
{
dunkComplete = true;
//set ball position move to game manager
//rb.velocity = new Vector2 (0, 0);
//transform.position = new Vector2(0, -3.5f);
//rb.is$$anonymous$$inematic = true;
//dunked = false;
//tempDunkTime = dunkTime;
//dunkComplete = false;
}
}
void Swipe ()
{
rb.is$$anonymous$$inematic = false;
Vector2 swipe = endSwipe - startSwipe;
swipe = swipe * force;
x_swipe = swipe.x;
x_swipe = x_swipe * -1;
Debug.Log(swipe);
rb.AddForce(swipe, Force$$anonymous$$ode2D.Impulse);
rb.AddTorque(x_swipe*torque, Force$$anonymous$$ode2D.Impulse);
}
void OnTriggerEnter2D(Collider2D col)
{
if(col.tag == "Finish")
{
dunked = true;
//rb.is$$anonymous$$inematic = true;
}
}
}
There it is
Answer by mrsam993 · Oct 07, 2017 at 08:24 PM
If anybody else has this problem i found the solution here http://answers.unity3d.com/questions/418609/how-do-i-check-if-bool-is-true-from-another-script.html