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 Jun 23, 2018 at 09:50 AM by Reun_.
avatar image
0
Question by Reun_ · Jun 21, 2018 at 11:49 AM · arraysvoid

Variable not accesible

In my program, a variable isn't available from a void.

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;

     public class GameManager : MonoBehaviour {
 
 
     [Space]
     [Header("Tiles")]
     public GameObject Tiles;
     public GameObject[,] tiles = new GameObject[8,8];
 
     
 
     public void Start()
     {
         for (int i = 0; i < 8; i++)
         {
             Transform parent = Tiles.transform.GetChild(i);
             for (int j = 0; j < 8; j++)
             {
                 tiles[i, j] = parent.GetChild(j).gameObject;
                 tiles[i, j].gameObject.GetComponent<Tile>().PosInput(i, j);
             }
         }
         tiles[3, 0].transform.position = new Vector3(0f, 10f, 0f);    //<-- Here the tiles[3,0] GameObject gets moved to (0f,10f,0f)
     }
     
    //On Mouse Click
     public void Input()
     {
           tiles[3, 0].transform.position = new Vector3(1f, 10f, 0f); //<-- But here it doesn't. It says the there's no GameObject in tiles[3,0]
     }

}

Comment
Add comment · Show 4
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 ShadyProductions · Jun 21, 2018 at 04:08 PM 0
Share

How are you calling the Input() method? $$anonymous$$ake sure Start() is called before the Input() method is called.

avatar image Reun_ ShadyProductions · Jun 22, 2018 at 08:44 AM 0
Share

Input is called from the Tile script like this:

 public Game$$anonymous$$anager G$$anonymous$$;
 
     public void On$$anonymous$$ouseDown()
     {
         G$$anonymous$$.Input(position[0],position[1], white, character);
     }

And the Input actually looks like this :

 public void Input(int i, int j, bool white, int character)
 {
     tiles[3, 0].transform.position = new Vector3(1f, 10f, 0f);
 }


avatar image tormentoarmagedoom · Jun 21, 2018 at 04:21 PM 0
Share

Good day.

What variable?

avatar image Reun_ · Jun 22, 2018 at 08:50 AM 0
Share

The problem is that inside the Input() $$anonymous$$ethod it can't access the change in the variables. Proof: [Header("Scripts")] public SetSpawner SS; public Figure$$anonymous$$oves F$$anonymous$$;

     [Space]
     [Header("Tiles")]
     public GameObject Tiles;
     public GameObject[,] tiles = new GameObject[8,8];
 
     [Space]
     [Header("Variables")]
     public bool whiteturn = true;
     public bool yes = false;
 
     
 
     public void Start()
     {
         for (int i = 0; i < 8; i++)
         {
             Transform parent = Tiles.transform.GetChild(i);
             for (int j = 0; j < 8; j++)
             {
                 tiles[i, j] = parent.GetChild(j).gameObject;
                 tiles[i, j].gameObject.GetComponent<Tile>().PosInput(i, j);
             }
         }
         SS.SpawnNormal();
         F$$anonymous$$.Input(tiles);
         TileFiguresN();
         yes = true;
     }
     public void Input(int i, int j, bool white, int character) // Called from Tile script On$$anonymous$$ouseDown()
     {
           Debug.Log(yes);
     }

The output in the console is : false

2 Replies

  • Sort: 
avatar image
0

Answer by belwar · Jun 22, 2018 at 10:33 AM

The problem is that the Input function isn't called when the mouse is clicked.

Use Update method instead:

     public void Update()
     {
         if (Input.GetMouseButtonDown(0))
             tiles[3, 0].transform.position = new Vector3(1f, 10f, 0f);
     }
Comment
Add comment · Show 4 · 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 Reun_ · Jun 22, 2018 at 10:42 AM 0
Share

If I put a Debug.Log("works") In the Input method it says "works" in the Console so the code is running but it can't find the tiles[] variable

avatar image belwar · Jun 22, 2018 at 10:47 AM 0
Share

Input function isn't called by default if an input event occurs.

Do you call it from another function? Please share it's code.

avatar image Reun_ belwar · Jun 22, 2018 at 10:55 AM 0
Share

it is called from the Tile scripts like this:

  public Game$$anonymous$$anager G$$anonymous$$;
  
      public void On$$anonymous$$ouseDown()
      {
          G$$anonymous$$.Input();
      }
 

avatar image Reun_ Reun_ · Jun 22, 2018 at 10:56 AM 0
Share

I'm leaving out some parts like in Input it sends its position, a bool, a character but that doesn't affect the code.

avatar image
0

Answer by Reun_ · Jun 23, 2018 at 09:49 AM

I've created a new problem but with everything in it please check that out: @belwar https://answers.unity.com/questions/1521174/variable-values-not-avalaible-or-they-lost-their-v.html

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

Follow this Question

Answers Answers and Comments

88 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

Related Questions

Simplifying code - similar array names 1 Answer

For loop not running correctly inside start () 1 Answer

Using arrays and for loop with GetComponent error 1 Answer

How to select a texture in an inventory? 0 Answers

How to load an inventory array? 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