Unity 2d having instantiate problems.

hey, so im having this weird error: Maybe you know how to fix it? :slight_smile: Object reference not set to an instance of an object. Here’s the script:

Fixed it.

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 ();
InvokeRepeating(“Spit”, 1.0F, 2F);

}
void Spit()
{
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);
}

}

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);

}
}

Thanks! :slight_smile: :-D:face_with_spiral_eyes:

Please use the code tags


It means you don’t have anything assigned to acid so it can’t instantiate whatever acid is supposed to be. Just drag the acid prefab/object into the acid field for the class.

Thank you, for the anwser. The problem is that i dragged my acid prefab into Inspector. Everything is done right in the Inspector. I have no idea whats wrong.;):face_with_spiral_eyes: