Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 27, 2017 at 02:12 AM by Agent27765.
avatar image
0
Question by Agent27765 · Sep 13, 2017 at 12:42 AM · script.setactiveboolif statement

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;
         }




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

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...

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by TheSOULDev · Sep 13, 2017 at 01:15 AM

You never defined swordOut in the scripts you provided us.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Agent27765 · Sep 14, 2017 at 09:17 PM 0
Share

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!

avatar image TheSOULDev Agent27765 · Sep 14, 2017 at 10:54 PM 0
Share

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.

avatar image Agent27765 TheSOULDev · Sep 14, 2017 at 11:18 PM 0
Share

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.

Show more comments

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges