Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Tomster954 · Nov 19, 2013 at 04:32 AM ·

How to access the value of a bool in another script c#?

script 1:

public bool Enabled;

if (Input.GetKeyUp(KeyCode.Escape)) {

Enabled = !Enabled;

}

script 2:

if (!Enabled) {

debug.log ("ESC has been pressed");

}

My problem is that the second script doesn't know what Enabled is. This is a very simplified version of my code but if this can be answered ill be able to implement it in my actual code. So basically what i want to do is have two different scrips but when Enabled changes to true or false i want it to also change the other script. Please answer the question if possible without the solution being put it all in one script.

Comment
Add comment · Show 3
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 Tomster954 · Nov 19, 2013 at 07:28 AM 0
Share

This is what ive got but i get the error - NullReferenceException: Object reference not set to an instance of an object snipershooting.Update () (at Assets/Standard Assets & $$anonymous$$ine/$$anonymous$$y Scripts/snipershooting.cs:23)

snipershooting is the script that controls how the sniper shoot.

using UnityEngine; using System.Collections; public class snipershooting : $$anonymous$$onoBehaviour { public bool GunEnabled; public GameObject Bullet; public GameObject muzzleflare; public GameObject shootingdirection; public Vector3 bulletoffset; Back$$anonymous$$enuScript Script1Object; void start () { Script1Object = GetComponent<Back$$anonymous$$enuScript>(); } // Update is called once per frame void Update () { if (Input.Get$$anonymous$$ouseButtonDown(0) && !Script1Object.Enabled) { bulletoffset = transform.position + (transform.up * 0f ) + ( transform.forward * 0f)+ ( transform.right * -0f ); Instantiate(Bullet, bulletoffset, shootingdirection.transform.rotation); Instantiate(muzzleflare , bulletoffset, shootingdirection.transform.rotation); } } }

this error only comes up when i put && !Script1Object.Enabled into the if statment.

avatar image Tomster954 · Nov 19, 2013 at 07:28 AM 0
Share

Assets/Standard Assets & $$anonymous$$ine/$$anonymous$$y Scripts/snipershooting.cs(23,58): error CS0176: Static member `Back$$anonymous$$enuScript.Enabled' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d i think if we fix this error things could never be better. i think this is the problem causing the other one.

avatar image tanoshimi · Nov 19, 2013 at 07:28 AM 0
Share

This is the single most-asked (and most-answered) question on this site... there's also a page dedicated to the subject in the manual and on the UnityGems site. Please search before asking.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by united4life · Nov 19, 2013 at 05:32 AM

First you have to access script1 in script2

 script2 
 ---------------------
 
 Script1 script1Object;
 
 void Start()
 {
 script1Object = GetComponent<Script1>();
 }
 
 void Example()
 {
 if(!script1Object.Enabled) {
 debug.log ("ESC has been pressed");
 }
 }
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 supernat · Nov 19, 2013 at 05:33 AM

There are several ways to do it. If script 1 will only ever exist on one object, make Enabled static. Then from script 2, you can use "Script1.Enabled".

Otherwise, you would need a reference to script 1. I.e. Script1 obj = GetScript1SomeHow(); Then use obj.Enabled

Comment
Add comment · Show 2 · 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 Tomster954 · Nov 19, 2013 at 06:35 AM 0
Share

hey i have the error - Assets/Standard Assets & $$anonymous$$ine/$$anonymous$$y Scripts/snipershooting.cs(26,58): error CS0176: Static member `Back$$anonymous$$enuScript.Enabled' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d. i think i accidentally asked for a solution to this error through an answer on this lel.

avatar image supernat · Nov 19, 2013 at 02:28 PM 0
Share

Static members are accessed via class while instances members are accessed via instance. You are trying to access a static member by an instance, so remove the keyword static or use the class name to access it ins$$anonymous$$d of a variable

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

how to get void function in c# script to other c#script? 2 Answers

Problem with Execution Order 0 Answers

how to make script when two cards hit and action? like solitaire.. 0 Answers

Updating PC Control code for mouse camera movement Doesn't work Help! 0 Answers

Whats wrong with this code (C# script)??? 3 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