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 Dec 05, 2020 at 07:38 AM by obaydaSSJ for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by obaydaSSJ · Sep 08, 2018 at 10:50 AM · arrayif statementbooleansreturn value

Array of bool doesn't change in other script

the code is :

 using UnityEngine;
         using UnityEngine.UI;
         public class InventorySlot : MonoBehaviour {    // Scrpit A
         
         public bool[] buttonState = new bool[2];
         
         void Update ()
             {
                 if (Input.GetKeyDown (KeyCode.Alpha1) || Input.mouseScrollDelta.y > 0)
                 {
                     buttonState [0] = true;
                                 buttonState [1] = false;
                 }
                     if (Input.GetKeyDown (KeyCode.Alpha2) || Input.mouseScrollDelta.y < 0)
                 {
                     buttonState [0] =  false;
                                 buttonState [1] = true;
                 }
     
     public bool ButtonIsActive (int index)
         {
             return  buttonState[index];  // true or false from the update funaction
         }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

  using System.Collections;
     using UnityEngine;
     
       public class CollectShiled : MonoBehaviour {   // Scrpit B
             public InventorySlot inventory;    // I put it in the incpector
     void Update ()
       {
           if (Input.GetKeyDown ("space") &&  inventory.ButtonIsActive(index))  // index =1 or 0 
                         {
                            // Do something 
                         }
         }

the problem is in the script A always return true !

     public bool ButtonIsActive (int index)
    {
       return  buttonState[index]; 
     }
Comment
Add comment · Show 2
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 myzzie · Sep 08, 2018 at 02:02 PM 0
Share

$$anonymous$$ight not be relevant but where's the variable index stored in your CollectShiled class? There seem to be some code missing as that wouldn't compile as is.

avatar image obaydaSSJ myzzie · Sep 08, 2018 at 06:10 PM 0
Share

Yes , variable index in CollectShiled class , but i removed unnecessary part to make it more clear if you want to see full code i will send it

 int index = Shiled.index; // where index is 1 or 0

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by $$anonymous$$ · Sep 08, 2018 at 11:51 AM

If you want it to be only true if the key is held down you have to Input.GetKey Instead and create an else statement that sets the bools to false.

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 obaydaSSJ · Sep 08, 2018 at 12:06 PM 0
Share

I want to this ButtonIsActive() to return the current value of buttonState[] but it's always return true !

avatar image $$anonymous$$ · Sep 08, 2018 at 01:40 PM 0
Share

So it does return true no matter what key you pressed? Have you tried Debug.Log the mouse.Scrolldata?

avatar image obaydaSSJ $$anonymous$$ · Sep 08, 2018 at 02:39 PM 0
Share

It's work correctly when i print the value from the script A and the mouse.scroll work correctly but when i call the function ButtonIsActive() from script B it's return true always . I think the problem in the array do you have any idea ?

avatar image $$anonymous$$ obaydaSSJ · Sep 08, 2018 at 06:12 PM 0
Share

I tried it on my pc and it works perfectly fine for me. If I press Alpha1 the element 0 gets true and element 1 gets false. If I press Alpha2 the element 1 gets false and element 1 gets true. I made a Debug if I press space and the current index is true, and it works.

What does the scene look like? Are the two scripts on the same gameobject? $$anonymous$$aybe you acess a disabled inventorySlot script and because of that the buttonState never changes? Or does maybe another script change the buttonState? I dont really see another reason why it should only return true...

Show more comments

Follow this Question

Answers Answers and Comments

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

[solved] c# script : get a return val (waypoint example) 2 Answers

Activating Bool Array in sequence. 2 Answers

I am referring a bool function from other script and i am checking with in if condition , but it is not working? can anyone can help me? 0 Answers

Return array of strings 1 Answer

Collecting items and store them in memory to place them later at different locations. 1 Answer


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