Dialogue & stop walking
I'm making a dialog script and I've made it instantiate, dialogue boxes and animations using a ++ system, and in the end the NPC is supposed to follow me (my grab function) which is not happening. Unity keeps saying I don't have the game object associated, which is probably the reason, but I deleted and put it again and it still does not work
void Update () {
if (Input.GetKeyUp(KeyCode.E) && isPressed) {
isPressed = false;
step++;
}
if(walking = false)
{
player.GetComponent<Walking>().enabled = false;
}
}
void OnTriggerStay2D (Collider2D other) {
if (grab==false) {
if (Input.GetKeyDown(KeyCode.E) && step==0)
{
player.GetComponent<Animator>().Play("bribe");
isPressed = true;
}
if (Input.GetKeyDown(KeyCode.E) && step==1)
{
coworker.GetComponent<Animator>().Play("cowAccept");
if (dialog==null) dialog = (GameObject) Instantiate(bubbleStory, new Vector3(20.71044f, 1.192236f, 0), Quaternion.identity);
Vector3 V4 = coworker.transform.position;
V4.x += 0.0f;
V4.y += 0.5f;
coworker.transform.position = V4;
isPressed = true;
}
if (Input.GetKeyDown(KeyCode.E) && step==2)
{
Destroy(dialog);
dialog = null;
coworker.GetComponent<Animator>().Play("cowWalk");
grab = true;
isPressed = true;
}
} else {
if (Input.GetButton("Jump"))
{
Vector3 V4 = player.transform.position;
V4.x += 1f;
V4.y += -0.5f;
coworker.transform.position = V4;
coworker.GetComponent<Animator>().Play("shieldBanker");
walking = false; //STOP WALKING
}
else if (Input.GetButtonUp("Jump"))
{
Destroy(coworker);
}
}
}
void OnTriggerExit2D(Collider2D other)
{
if (grab==true){
if (coworker!=null) {
//Debug.Log(coworker.transform.position);
Vector3 V4 = player.transform.position;
V4.x += -0.5f;
V4.y += 0.0f;
coworker.transform.position = V4;
}
}
}
}
besides that I'm also trying to make the player not being able to walk when using the shield so I used the
if(walking = false)
{
player.GetComponent<Walking>().enabled = false;
}
and then call it false at the right time?
Answer by Hakaysha · Jan 30, 2016 at 09:54 PM
If someone is having this stupid problem, which apparently was unity's fault as it wasn't identifying the player in the scene, instead of directly referencing it, like I was doing, with public GameObject player;
just use player = GameObject.Find("player");
as it helped clean up a lot of bugs I was having because of "dragg and drop" referencing.
Your answer
![](https://koobas.hobune.stream/wayback/20220612062249im_/https://answers.unity.com/themes/thub/images/avi.jpg)