- Home /
Question is off-topic or not relevant
Something is wrong with my code please help!
Hey! I try to make a little game for an event in our school but something doesn't work in my code. When I try to access to another script's bool it doesn't care of it. I want to read it and write it also but non of them works. Script 1 (with the bool): using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BarrelTarget : MonoBehaviour { public GameObject target; public bool IsThere; public GameObject spawn;
void Start ()
{
IsThere = false;
}
void Update()
{
if (IsThere == false)
{
print("false");
StartCoroutine(SpawnTarget());
IsThere = true;
}
}
private IEnumerator SpawnTarget()
{
yield return new WaitForSeconds(Random.Range(0.0f, 5.0f));
Instantiate(target, spawn.transform.position, gameObject.transform.rotation);
}
Script 2 (tries to access it): using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TargetBarrel : MonoBehaviour { private Rigidbody rb; private float Force = 200f; private GameObject cam; private float fireRate = 0.25f; private float nextFire; public GameObject destroyedVersion; public Transform Pointa; public Transform Pointb; private Vector3 PointA; private Vector3 PointB; public float Speed = 0.5f; public GameObject barrel; public BarrelTarget script;
void Start()
{
rb = GetComponent<Rigidbody>();
PointA = new Vector3(Pointa.transform.position.x, Pointa.transform.position.y, Pointa.transform.position.z);
PointB = new Vector3(Pointb.transform.position.x, Pointb.transform.position.y, Pointb.transform.position.z);
cam = GameObject.Find("Main Camera");
script = barrel.GetComponent<BarrelTarget>();
}
void Update()
{
if (script.IsThere)
{
transform.position = Vector3.Lerp(PointA, PointB, Mathf.PingPong(Time.fixedTime * Speed, 1.0f));
}
}
void OnMouseOver()
{
if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
rb.AddForce(cam.transform.forward * Force);
Instantiate(destroyedVersion, transform.position, transform.rotation);
script.IsThere = false;
Destroy(gameObject);
}
}
Format correctly your code! (use the 101010 button)
Take a look at UnityAnswers' FAQ before posting anything: http://answers.unity3d.com/page/faq.html
What doesn't work? Are you getting an error? Be more specific about your problem.
Do you have any error?
Show us how you declared the variables.
Try to use Debug.Log (script.IsThere);
Before your if (script.IsThere) put if(script.IsThere == null) { Debug.Log("Script is null"); return; } to check if it is finding the script.
Also put an if statement that checks if script.IsThere is == false debug. Or a debug in the first script to make sure the bool is actually being changed to true
Usually I have this problem when starting new Unity session. Do you have both scripts attached to one gameObject?
What exactly is the issue you're having - is there an error of some kind, or is it not doing what you expect it to do? The only problem I can see is that if you are not assigning anything to the 'public GameObject barrel' at the start of your script, then the script wont do what you want it to.
If you're instantiating 'TargetBarrel' then you need to add some way to assign a gameobject to 'barrel' in the class 'TargetBarrel', since your class doesn't contain the code to do so.
Otherwise it must be an issue with the logic in your code.