• Unity 2d Instantiate problems
    4 replies, posted
hey, so im having this weird error: [B]Object reference not set to an instance of an object[/B]. Here's the script: using UnityEngine; using System.Collections; public class AcidZombieControlls : MonoBehaviour { public float X; public float speed; public bool paperStyle; public bool scaleStyle; private Transform myTransform; public float moveSpeed; public Transform target; public Transform SpitAreaStart; public Transform SpitAreaEnd; public bool spotted = false; Animator anim; public Transform Acid; public Transform AcidSpawnPos; public bool ImFacingLeft; public float timeBetweenShots = 5.0f; void Start () { X = transform.localScale.x; anim = GetComponent <Animator> (); InvokeRepeating("Spit", 1.0F, 2F); } void Spit() { [B]if(ImFacingLeft) { GameObject acid = Instantiate (Acid, AcidSpawnPos.position, AcidSpawnPos.transform.rotation) as GameObject; acid.rigidbody2D.AddForce (Vector2.right * -200); } if(!ImFacingLeft) { GameObject acid = Instantiate (Acid, AcidSpawnPos.position, AcidSpawnPos.transform.rotation) as GameObject; acid.rigidbody2D.AddForce (Vector2.right * 200); } [/B] } void Update () { if(target.position.x < myTransform.position.x) { ImFacingLeft = true; } if(target.position.x > myTransform.position.x) { ImFacingLeft = false; } Vector3 oldEulers = myTransform.eulerAngles; if (target.position.x < myTransform.position.x) { myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; //We're checking inside a threshold so the object doesn't rapidly flip when even with the player. if (target.position.x < myTransform.position.x-0.02f) { if (paperStyle) { oldEulers.y = Mathf.LerpAngle(oldEulers.y, 0, 0.25f); } else if (scaleStyle) { transform.localScale = new Vector3(-1, 1, 1); } else { oldEulers.y = 0; } } }// player is left of enemy, move left if (target.position.x > myTransform.position.x) { //If we're using either rotate style, transform.right will be relative. if (scaleStyle && !paperStyle) { myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; } else { myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; } //We're checking inside a threshold so the object doesn't rapidly flip when even with the player. if (target.position.x > myTransform.position.x + 0.02f) { if (paperStyle) { oldEulers.y = Mathf.LerpAngle(oldEulers.y, 180, 0.25f); } else if (scaleStyle) { transform.localScale = new Vector3(1, 1, 1); } else { oldEulers.y = 180; } } }// player is right of enemy, move right transform.eulerAngles = oldEulers; Raycasting(); Behaiviours(); } void Raycasting() { Debug.DrawLine (SpitAreaStart.position, SpitAreaEnd.position, Color.green); spotted = Physics2D.Linecast (SpitAreaStart.position, SpitAreaEnd.position, 1 << LayerMask.NameToLayer ("Player")); if(spotted == true) { anim.SetBool ("Acid", true); speed = 0; } if(!spotted) { anim.SetBool ("Acid", false); speed = 0.1f; } } void Behaiviours() { } void Awake () { myTransform = transform; } void FixedUpdate () { float step = speed * Time.deltaTime; transform.position = Vector3.MoveTowards(transform.position, target.position, step); } }
[QUOTE=Matkenis;44833768]hey, so im having this weird error: [B]Object reference not set to an instance of an object[/B]. Here's the script: [code]using UnityEngine; using System.Collections; public class AcidZombieControlls : MonoBehaviour { public float X; public float speed; public bool paperStyle; public bool scaleStyle; private Transform myTransform; public float moveSpeed; public Transform target; public Transform SpitAreaStart; public Transform SpitAreaEnd; public bool spotted = false; Animator anim; public Transform Acid; public Transform AcidSpawnPos; public bool ImFacingLeft; public float timeBetweenShots = 5.0f; void Start () { X = transform.localScale.x; anim = GetComponent <Animator> (); InvokeRepeating("Spit", 1.0F, 2F); } void Spit() { // [B] if(ImFacingLeft) { GameObject acid = Instantiate (Acid, AcidSpawnPos.position, AcidSpawnPos.transform.rotation) as GameObject; acid.rigidbody2D.AddForce (Vector2.right * -200); } if(!ImFacingLeft) { GameObject acid = Instantiate (Acid, AcidSpawnPos.position, AcidSpawnPos.transform.rotation) as GameObject; acid.rigidbody2D.AddForce (Vector2.right * 200); } // [/B] } void Update () { if(target.position.x < myTransform.position.x) { ImFacingLeft = true; } if(target.position.x > myTransform.position.x) { ImFacingLeft = false; } Vector3 oldEulers = myTransform.eulerAngles; if (target.position.x < myTransform.position.x) { myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; //We're checking inside a threshold so the object doesn't rapidly flip when even with the player. if (target.position.x < myTransform.position.x-0.02f) { if (paperStyle) { oldEulers.y = Mathf.LerpAngle(oldEulers.y, 0, 0.25f); } else if (scaleStyle) { transform.localScale = new Vector3(-1, 1, 1); } else { oldEulers.y = 0; } } }// player is left of enemy, move left if (target.position.x > myTransform.position.x) { //If we're using either rotate style, transform.right will be relative. if (scaleStyle && !paperStyle) { myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; } else { myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; } //We're checking inside a threshold so the object doesn't rapidly flip when even with the player. if (target.position.x > myTransform.position.x + 0.02f) { if (paperStyle) { oldEulers.y = Mathf.LerpAngle(oldEulers.y, 180, 0.25f); } else if (scaleStyle) { transform.localScale = new Vector3(1, 1, 1); } else { oldEulers.y = 180; } } }// player is right of enemy, move right transform.eulerAngles = oldEulers; Raycasting(); Behaiviours(); } void Raycasting() { Debug.DrawLine (SpitAreaStart.position, SpitAreaEnd.position, Color.green); spotted = Physics2D.Linecast (SpitAreaStart.position, SpitAreaEnd.position, 1 << LayerMask.NameToLayer ("Player")); if(spotted == true) { anim.SetBool ("Acid", true); speed = 0; } if(!spotted) { anim.SetBool ("Acid", false); speed = 0.1f; } } void Behaiviours() { } void Awake () { myTransform = transform; } void FixedUpdate () { float step = speed * Time.deltaTime; transform.position = Vector3.MoveTowards(transform.position, target.position, step); } }[/code][/QUOTE] Use [noparse][code][/code][/noparse] tags. [editline]18th May 2014[/editline] [code][b]bold[/b] [editline]editline[/editline][/code] [editline]18th May 2014[/editline] Oh wow, I never noticed :v:
Do you get a line number with the error? [editline]17th May 2014[/editline] Or I guess it is at the bolded part.
I assume it's the use of [code]as[/code] here, try with a normal cast and check if the conversion fails.
Yep, looks like Acid is a transform rather than a GameObject and so the clone will become null after conversion.
Sorry, you need to Log In to post a reply to this thread.