could someone please explain why the two Debug.log are different when they are on the same GameObject and even in the same script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bossLeftRight : MonoBehaviour {
public float leftLimit;
public float rightLimit;
bool movingLeft = false;
public float speed;
public float distBuffer;
Vector3 targetPosition;
// Use this for initialization
void Start () {
targetPosition = new Vector3 (Random.Range (leftLimit, rightLimit), transform.position.y, transform.position.z);
}
// Update is called once per frame
void Update () {
Debug.Log ("Raw Position + " + transform.position);
transform.position = Vector3.Lerp (transform.position, targetPosition, speed * Time.deltaTime);
// if (transform.position.x < targetPosition.x - distBuffer || transform.position.x > targetPosition.x + distBuffer) {
if (transform.position.x < targetPosition.x + distBuffer && movingLeft == false) {
targetPosition = new Vector3 (Random.Range (leftLimit, rightLimit), transform.position.y, transform.position.z);
movingLeft = true;
}
if (transform.position.x > targetPosition.x - distBuffer && movingLeft == true) {
targetPosition = new Vector3 (Random.Range (leftLimit, rightLimit), transform.position.y, transform.position.z);
movingLeft = false;
}
}
public Vector3 returnBossPos(){
Debug.Log ("Pos + " + transform.position);
return transform.position;
}
}
I don’t see where you are calling returnBossPos() at?
its called in a different script
public void EnemyFire(){
firePoint = this.GetComponent<bossLeftRight> ().returnBossPos ();
GameObject spawnEnemyBullet = Instantiate (BossBullet, firePoint, Quaternion.identity);
spawnEnemyBullet.GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, bossBulletSpeed);
}
So since you’re updating and getting a transform at different times it can give you different results then you expect.
If for example, this runs first
int test = 0;
void Update(){
Debug.Log ("Raw Position + " + test); //Test prints 0
test++; //Test is now 1
}
and then this runs
public void EnemyFire(){
Debug.Log ("Raw Position + " + test);//Test now shows 1
}
Yeah I understand how they can be slightly off. The debug position in the update changes but the one in the returnBossPos() never changes.
Oh, I could see how that would cause confusion. I called returnBossPos() just in the update function and it worked fine for me.
Try caching bossLeftRight and see if that helps.
//What ever name you want
bossLeftRight t;
public Start()
{
t = this.GetComponent<bossLeftRight> ();
}
public void EnemyFire(){
firePoint = t.returnBossPos ();
GameObject spawnEnemyBullet = Instantiate (BossBullet, firePoint, Quaternion.identity);
//You should do the same for the ridgibody and cache it too.
//That way you're not calling getComponent over and over
spawnEnemyBullet.GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, bossBulletSpeed);
}
Hmmm I declared the bossLeftRight Script on start. Now I am getting A NullReferenceException at BossManager.cs line 39. Both scripts are attached to the boss and both are active. I am not really sure why I would get a null exception.
BossLeftRight
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bossLeftRight : MonoBehaviour {
public float leftLimit;
public float rightLimit;
bool movingLeft = false;
public float speed;
public float distBuffer;
Vector3 targetPosition;
void Start () {
targetPosition = new Vector3 (Random.Range (leftLimit, rightLimit), transform.position.y, transform.position.z);
}
void Update () {
Debug.Log ("Raw Position + " + transform.position);
transform.position = Vector3.Lerp (transform.position, targetPosition, speed * Time.deltaTime);
if (transform.position.x < targetPosition.x + distBuffer && movingLeft == false) {
targetPosition = new Vector3 (Random.Range (leftLimit, rightLimit), transform.position.y, transform.position.z);
movingLeft = true;
}
if (transform.position.x > targetPosition.x - distBuffer && movingLeft == true) {
targetPosition = new Vector3 (Random.Range (leftLimit, rightLimit), transform.position.y, transform.position.z);
movingLeft = false;
}
}
public Vector3 returnBossPos(){
Debug.Log ("Pos + " + transform.position);
return transform.position;
}
}
Boss Manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossManager : MonoBehaviour {
int bossHealth = 100;
public GameObject BossBullet;
public float bossBulletSpeed;
GameManager gameManager;
Vector3 bossStartPos;
bool bossMovetoCenter = false;
bool flyOffScreen = false;
Vector3 flyOffScreenPoss;
Vector3 firePoint;
bossLeftRight bLeftRight;
Rigidbody2D bulletRigidBody2D;
void Start(){
bLeftRight = this.GetComponent<bossLeftRight> ();
bossStartPos = transform.position;
gameManager = GameObject.Find ("GM").GetComponent<GameManager> ();
}
void Update(){
if (bossMovetoCenter == true) {
transform.position = Vector3.Lerp (transform.position, bossStartPos, Time.deltaTime * 3);
}
if (flyOffScreen == true) {
transform.position = Vector3.Lerp (transform.position, flyOffScreenPoss, Time.deltaTime * 3);
}
}
public void EnemyFire(){
firePoint = bLeftRight.returnBossPos ();
GameObject spawnEnemyBullet = Instantiate (BossBullet, firePoint, Quaternion.identity);
spawnEnemyBullet.GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, bossBulletSpeed);
}
void OnTriggerEnter2D(Collider2D coll){
if(coll.CompareTag("Balls")){
bossHealth = bossHealth - 5;
GameObject.Find ("GM").GetComponent<Score> ().updateScore (800);
}
if (50 <=bossHealth && bossHealth <= 75) {
gameObject.GetComponent<Animator> ().SetTrigger ("BossDamageAnim");
}
else if (75 <=bossHealth && bossHealth <= 75) {
gameObject.GetComponent<Animator> ().speed = 2;
}
else if (0 <=bossHealth && bossHealth <= 25) {
gameObject.GetComponent<Animator> ().speed = 5;
}
if (bossHealth <= 0) {
KillBoss ();
}
}
void KillBoss(){
StartCoroutine (gameManager.BossLost());
}
public void MoveBossToCenter(){
gameObject.GetComponent<bossLeftRight> ().enabled = false;
bossMovetoCenter = true;
}
public void bossFlyOffScreen(){
flyOffScreenPoss = new Vector3 (bossStartPos.x + 2, bossStartPos.y, bossStartPos.z);
bossMovetoCenter = true;
}
}
Anyone have any idea about this. For the life of me I can not figure it out.
Throw a debug statement into your Start function to find out if “bLeftRight” comes back null from the GetComponent call.
Just so you know, the common convention is to have CapitalCase class names. Also putting a “b” in front of a variable name usually indicates that it is a boolean type.
Well I place Debug.Log(bLeftRight) in the start and I get back. Boss UFO Body(Clone)(bossLeftRight) so how could I be getting a null return still?
Thanks for the Tips. I will keep those in mind.
ok so if I change
firePoint = bLeftRight.returnBossPos ();
to
firepoint = GameObject.FindGameObjectWithTag(“Boss”).GetComponent().returnBossPos()
it works. I dont understand why when I assign it in start it will even say it is not a null through a debug.log test but then it gives a null later in the code.
I really do not want to run GameObject.Find every time a bullet is fired
You must have more than one “Boss” object in the scene, and the Find method is getting the correct one. You must have another one in there somewhere without the component. That’s the only thing I can think of if you’re not removing components somewhere.