Accessing a variable using Get Component from other GameObject Script
I have two scripts.
One script controls the player's movement and detects the collision between GameObjects such as NPCs, doors, etc:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 6.0f;
public float turnSpeed = 60.0f;
public float gravity = 20.0f;
public bool canDo;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
private Animator anim;
void Start ()
{
controller = GetComponent<CharacterController> ();
anim = GetComponent<Animator> ();
}
void Update ()
{
if (canDo == true)
{
if (controller.isGrounded)
{
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0.0f, Input.GetAxis ("Vertical"));
}
Vector3 facingrotation = Vector3.Normalize (new Vector3 (Input.GetAxis ("Horizontal"), 0f, Input.GetAxis ("Vertical")));
if (facingrotation != Vector3.zero)
{
transform.forward = facingrotation;
}
controller.Move (moveDirection * speed * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
if (Input.GetKey ("up") || Input.GetKey ("down") || Input.GetKey ("left") || Input.GetKey ("right"))
{
anim.SetInteger ("isMoving", 1);
} else {
anim.SetInteger ("isMoving", 0);
}
}
}
void OnControllerColliderHit (ControllerColliderHit other)
{
if (other.gameObject.CompareTag ("Door"))
{
if (Input.GetKeyDown ("space"))
{
other.gameObject.SetActive (false);
}
}
if (other.gameObject.CompareTag ("NPC"))
{
if (Input.GetKeyDown ("space"))
{
}
}
}
}
And my other script, which controls what the NPC says (I have another already functioning script that does dialogue). It detects which order the text should be displayed based upon the variable whichDialogueBox:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCTestSpeechController : MonoBehaviour {
public DisplayTextController myDisplayTextController;
public GameObject dialogueSystem;
public PlayerController myController;
void Start ()
{
myDisplayTextController.whichDialogueBox = 0;
}
void Update ()
{
if (myDisplayTextController.whichDialogueBox == 1)
{
myController.canDo = false;
dialogueSystem.SetActive (true);
myDisplayTextController.nameString = "Jimothy";
myDisplayTextController.speechString = "'Ello! 'ello!";
}
if (myDisplayTextController.whichDialogueBox == 2)
{
dialogueSystem.SetActive (false);
myDisplayTextController.whichDialogueBox = 0;
myController.canDo = true;
}
}
}
How do I go about accessing the scripts variable whichDialogueBox and changing it. This would need to be changeable so that it can be used for all initiation of NPC action. I thought GetComponent may work, accessing the script after checking the tag to see if it's an NPC and then editing the variable, but I can't figure it out. I ask for a bit of help and suggestions of how to go about this. Am I doing this wrong? Thanks!
Answer by Hellium · Feb 20, 2018 at 09:57 AM
I would save a reference to the npcSpeechController
hit by your controller, and call the appropriate function in the Update
since OnControllerColliderHit
is called one frame only
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// ...
private NPCTestSpeechController npcSpeechController;
void Update ()
{
if (canDo == true)
{
// ...
if( npcSpeechController != null )
{
// Make sure the npcSpeechController is still in range
// Change the '10' threshold according to your needs
if( (npcSpeechController.transform.position - transform.position).sqrMagnitude > 10 )
npcSpeechController = null ;
else if( Input.GetKeyDown ("space") )
npcSpeechController.whichDialogueBox++ ;
}
}
}
void OnControllerColliderHit (ControllerColliderHit other)
{
if (other.gameObject.CompareTag ("Door"))
{
if (Input.GetKeyDown ("space"))
{
other.gameObject.SetActive (false);
}
}
else
{
// May be null, but it does not matter
// You may need to use GetComponentInParent instead of GetComponentInChildren
npcSpeechController = other.GetComponentInChildren<NPCTestSpeechController>();
}
}
}
I can see this, yes. This may work. I'm trying to access a general script by using get component so I only need to write something of this fashion once, and then trigger whatever the NPC does. I'll try this. Thanks.
It worked! Thank you very much! A quick question: How do I make it so you don't have to continuously walk into the NPC?
What do you mean by "continuously walk into the NPC"?
I have to continuously press the arrow keys as I collide with the NPC to register the trigger.