This seems to be a common error newbies keep getting, and I’d like to learn how to avoid this one properly as I’m always getting caught out by it.
I’m not sure what I’m missing to get this error, but I have a method in one script being referenced in another and calls it when needed. I have the script to be referenced (in this case, HutTrigger) as a variable at the top, I have GetComponent in the Start function, and then the method is called under the ControllerDetection function if the Xbox 360 controller is detected. The required game objects have been placed in the boxes the script is attached to, so I’m not sure what I’m missing to cause this error.
PlayerController script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public Animator anim;
public float moveSpeed;
public float jumpForce;
public bool jumped;
public bool attack;
public float gravityScale;
public float knockBackForce;
public float knockBackTime;
public float invincibilityLength;
public Renderer playerRenderer;
public Material textureChange;
public Material textureDefault;
public bool allowCombat = false;
public bool multipleDirections = false;
public float rotateSpeed;
public HutTrigger hutTrigger;
private float jumpDelay;
private Vector3 moveDirection;
private Vector3 extraDirections;
private float knockBackCounter;
private float invincibilityCounter;
private CharacterController controller;
private int xbox360Controller = 0;
private int ps4Controller = 0;
void Start()
{
Cursor.visible = false;
controller = GetComponent<CharacterController>();
hutTrigger = GetComponent<HutTrigger>();
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1"))
{
allowCombat = true;
multipleDirections = false;
}
else if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))
{
allowCombat = false;
multipleDirections = false;
}
else if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
{
allowCombat = false;
multipleDirections = true;
}
}
void Update()
{
if (knockBackCounter <= 0)
{
float moveHorizontal = Input.GetAxis("Horizontal");
moveDirection = new Vector3(moveHorizontal * moveSpeed, moveDirection.y);
if (moveHorizontal > 0)
{
transform.eulerAngles = new Vector3(0, 90);
}
else if (moveHorizontal < 0)
{
transform.eulerAngles = new Vector3(0, -90);
}
if (controller.isGrounded)
{
moveDirection.y = -1f;
//GetKeyDown will require the player to press the button each time they want to jump. GetKey will allow the player to spam the jump button if they keep pressing it down.
if (Input.GetKeyDown(KeyCode.KeypadPlus) || Input.GetKeyDown("joystick button 1"))
{
moveDirection.y = jumpForce;
jumped = true;
}
else if (!Input.GetKeyDown(KeyCode.KeypadPlus) || !Input.GetKeyDown("joystick button 1"))
{
jumped = false;
}
if (allowCombat)
{
if (Input.GetKey(KeyCode.Space) || Input.GetKey("joystick button 7"))
{
attack = true;
playerRenderer.material = textureChange;
}
else if (!Input.GetKey(KeyCode.Space) || !Input.GetKey("joystick button 7"))
{
attack = false;
playerRenderer.material = textureDefault;
}
}
else if (!allowCombat)
{
attack = false;
playerRenderer.material = textureDefault;
}
}
}
else
{
knockBackCounter -= Time.deltaTime;
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
anim.SetBool("isGrounded", controller.isGrounded);
anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
if (attack)
{
anim.SetTrigger("Attack");
}
}
public void ControllerDetection()
{
string[] names = Input.GetJoystickNames();
for (int x = 0; x < names.Length; x++)
{
//print(names[x].Length);
if (names[x].Length == 19)
{
//print("PS4 CONTROLLER IS CONNECTED");
ps4Controller = 1;
xbox360Controller = 0;
}
if (names[x].Length == 33)
{
//print("XBOX 360 CONTROLLER IS CONNECTED");
ps4Controller = 0;
xbox360Controller = 1;
}
if (xbox360Controller == 1)
{
hutTrigger.Xbox360Prompts();
}
else if (ps4Controller == 1)
{
}
else
{
}
}
}
public void Knockback(Vector3 direction)
{
knockBackCounter = knockBackTime;
moveDirection = direction * knockBackForce;
moveDirection.y = knockBackForce;
}
/*public Vector3 GetTravelDirection()
{
return moveDirection.normalized;
}*/
}
HutTrigger script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class HutTrigger : MonoBehaviour
{
public static bool hutLoaded;
public GameObject[] buttonPrompts;
public bool entranceVicinity;
private PlayerController playerController;
void Start()
{
playerController = FindObjectOfType<PlayerController>();
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
entranceVicinity = true;
playerController.ControllerDetection();
//buttonPrompt1.SetActive (true);
//Invoke("Hide", 3f);
}
}
void Update()
{
if (entranceVicinity)
{
if (Input.GetKeyDown("joystick button 2"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 2);
hutLoaded = true;
}
}
}
public void Hide()
{
buttonPrompts[0].SetActive (false);
buttonPrompts[2].SetActive(false);
}
public void Xbox360Prompts()
{
buttonPrompts[0].SetActive(true);
Invoke("Hide", 3f);
}
public void PCPrompts()
{
buttonPrompts[2].SetActive(true);
Invoke("Hide", 3f);
}
}
Can someone help me clear up the confusion? Thanks.