- Home /
Can't seem to get bool/SetActive Script to work
Hello. Recently I have been trying to make scripts with my own brain and not copy off other tutorials, But in the process I have been going through many problems. One of the problems I am having with right now is to get this bool/SetActive script to work. What I am trying to do is that when the character finds a certain chest and interacts with the chest it will set the givensword bool to true. After that I have a script seeing if I click Alpha1 what will happen is it will check if the givensword is true and if soo it will set the SetActive of the sword to true. What I found is that when I do not add the check to see if givensword is true then the script will work, But if I do add the givensword check it will say "Null reference not set to an instance of the object" I hope that I can figure out this problem fast so I can continue on with this game. Thank you for taking the time in helping me!
The Chest Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chest : ActionItem {
[HideInInspector]
public bool givensword = false;
public override void Interact()
{
Debug.Log("Interacting with Chest");
givensword = true;
}
}
Sword SetActive Script
public GameObject Sword;
Chest swordHasBeenFound;
//Take out Sword
if (Input.GetKey(KeyCode.Alpha1) && swordOut == false && swordHasBeenFound.givensword == true)
{
Sword.SetActive(true);
swordOut = true;
}
else
if(Input.GetKey(KeyCode.Alpha1) && swordOut == true && swordHasBeenFound.givensword == true)
{
Sword.SetActive(false);
swordOut = false;
}
Answer by ransomink · Sep 15, 2017 at 01:31 AM
swordHasBeenFound
is of type Chest. Since it is a private variable you need to set it inside of your script, preferably in Awake() or Start(). It's not set which is why you're getting a null reference error when attempting to access it...
Answer by TheSOULDev · Sep 13, 2017 at 01:15 AM
You never defined swordOut in the scripts you provided us.
Hey so for the swordOut I did have a private bool swordOut but I just forgot to add it into the script. Do you know any other possible reasons for my problem. Thanks!
Did you define the swordHasBeenFound of type Chest anywhere? It would really help if you provided the whole script in which there is a problem, as well as the error line and column.
Here is the full script of the second one
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WorldInteractions : $$anonymous$$onoBehaviour {
public GameObject Sword;
Chest swordHasBeenFound;
private bool foundSword;
private bool swordOut = false;
UnityEngine.AI.Nav$$anonymous$$eshAgent playerAgent;
void Start()
{
playerAgent = GetComponent<UnityEngine.AI.Nav$$anonymous$$eshAgent>();
}
//Setting Up Click
void Update ()
{
if (Input.Get$$anonymous$$ouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
GetInteractions();
//Take out Sword
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha1) && swordOut == false && swordHasBeenFound.givensword == true)
{
Sword.SetActive(true);
swordOut = true;
}
else
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha1) && swordOut == true && swordHasBeenFound.givensword == true)
{
Sword.SetActive(false);
swordOut = false;
}
}
//What Happens onClick
void GetInteractions()
{
Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit interactionInfo;
if(Physics.Raycast(interactionRay, out interactionInfo, $$anonymous$$athf.Infinity))
{
GameObject InteractedObject = interactionInfo.collider.gameObject;
if(InteractedObject.tag == "Interactable Object")
{
InteractedObject.GetComponent<Interactable>().$$anonymous$$oveToInteraction(playerAgent);
}
else
{
playerAgent.stoppingDistance = 0f;
playerAgent.destination = interactionInfo.point;
}
}
}
}
the first script is already the full one.
Follow this Question
Related Questions
I'm having trouble setting a bool. 2 Answers
physics.OverlapSphere colliders 1 Answer
check if the bool is true from other script c# 1 Answer
How do I check a bool from another script?? 2 Answers
VR Player enters trigger won't work 0 Answers