SOLVED Just need to use my head…
the line of code I was after is below. Put inside the If “AIPED” hit and Q for desired effect.
hit.transform.gameObject.GetComponent().state = AI.Status.Stop;
Hey guys - I’ve tried to play around and get it to work, but I guess I’m still having trouble.
I’m currently trying to get a single AI thats walking around if hit with a raycast from my player - I keep getting the ol ‘Object reference not set to an instances of an object’ I’m a bit confused how to handle this - normally it wouldn’t be a problem because there already on the board, but I’m spawning the AI in using a spawn point - so I’m a bit confused. I’ll show some code. Normal situations I don’t have a problem changing, or storing variables form other scripts - also is this the best way to change the state of the current AI (using a Enum)
Raycast is inside the player, and works fine - I just fail to grab the instant of the object I’m interacting with…
public Vector3 conductStop;
void Update ()
{
conductStop = transform.position;
conductStop.y += .5f;
RaycastHit hit;
Debug.DrawRay(conductStop,transform.forward * 5, Color.red);
if (Physics.Raycast(conductStop,transform.forward, out hit, 5))
{
if (hit.collider.gameObject.tag == "AIPED" && Input.GetKeyDown (KeyCode.Q))
{
Transform curPED;
curPED = gameObject.transform;
AI PED = gameObject.GetComponent<AI>();
PED.state = AI.Status.Stop;
}
All thats currently in this status of my AI is stopping speed until I figure out this reference issue I’m struggling with.
if (state == Status.Stop)
{
if (speed != 0)
{
speed = 0;
}
}