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 /
avatar image
0
Question by Reun_ · Jun 23, 2018 at 09:47 AM · arrayvariablesvaluesvoid

Variable values not available or they lost their value

In my program in the Input method, it can access the variables name but it can't access its value. Here the program: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class GameManager : MonoBehaviour {
 
 
     #region variables // Every variable is set in editor 
     [Header("Scripts")]
     public SetSpawner SS;
     public FigureMoves FM;
     
 
     [Space]
     [Header("Tiles")]
     public GameObject Tiles;
     public GameObject[,] tiles = new GameObject[8,8]; // this is the only variable not set in the editor
 
 
     [Space]
     [Header("Variables")]
     public bool whiteturn = true;
     public bool yes = false; 
     #endregion
 
 
     public void Start() // Unity calls this Method
     {
         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(); // This doesn't use any variables from here.
         FM.Input(tiles); // this just sends the tiles array so later on FM can use this
         yes = true;
         TileFiguresN(); // method is later shown
     }
     
 
     public void Input(int i, int j, bool white, int character) // Called from Tile script OnMouseDown()  Tile script shown later.
     { // THE PROBLEM IS HERE!
         string a;
         a = yes.ToString();
         bool b = true;
         if (Tiles == null)
         {
             b = false;
         }
         a = a + b.ToString();
         b = true;
         if (tiles[0,0] == null)
         {
             b = false;
         }
         a = a + b.ToString();
         b = true;
         if (FM == null)
         {
             b = false;
         }
         a = a + b.ToString();
         Debug.Log(a); // The output is FalseFalseFalseFalse
     }
 
     
     void TileFiguresN() // This doesn't matter I just put it so you can see the variables doesn't get changed.
     {
         for (int i = 0; i < 8; i++)
         {
             for (int j = 0; j < 8; j++)
             {
                 bool _white;
                 int character = 0;
                 if (i == 1 || i == 0)
                 {
                     _white = true;
                 }
                 else
                 {
                     _white = false;
                 }
                 if (i == 1 || i == 6)
                 {
                     character = 1;
                 }
                 if (i == 0 || i == 7)
                 {
                     if (j == 0 || j == 7)
                     {
                         character = 2;
                     }
                     if (j == 1 || j == 6)
                     {
                         character = 3;
                     }
                     if (j == 2 || j == 5)
                     {
                         character = 4;
                     }
                     if (j == 3)
                     {
                         character = 5;
                     }
                     if (j == 4)
                     {
                         character = 6;
                     }
 
                 }
                 tiles[i, j].GetComponent<Tile>().Input(_white, character);
             }
         }
     } //Set's characters to tiles
 }


In the console the output is FalseFalseFalseFalse So the variables lost their value. Everything in the Start method and TileFiguresN method is done and correct I've checked it multiple times.

The Tile script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Tile : MonoBehaviour {
 
     public GameManager GM;
     public int[] position = new int[2];
     public bool white;
     public int character;
     public Material avalaible;
     public Material original;
     
     public void Start()
     {
         original = gameObject.GetComponent<Renderer>().material;
     }
 
     public void OnMouseDown() //This is the method that calls the INPUT
     {
         GM.Input(position[0],position[1], white, character);
     }
 
     public void PosInput(int i, int j)
     {
         position[0] = i;
         position[1] = j;
     }
 
     public void Input(bool _white, int _character)
     {
         character = _character;
         white = _white;
     }
 
     public void Color(bool a)
     {
         if (a)
         {
 
             gameObject.GetComponent<Renderer>().material = avalaible; 
         }
         else
         {
             gameObject.GetComponent<Renderer>().material = original;
         }
     }
 
     public int ActiveCh()
     {
         return character;
     }
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JDelekto · Jun 23, 2018 at 10:52 AM

I think this is because you are using a field initializer to set your tiles[] array which you are not editing You can assign it in your Start() method to initialize it.

Since you are not using the editor to set the tiles[] array, try this:

    public void Start() // Unity calls this Method
    {
       this.tiles = new GameObject[8, 8];
       // ...   rest of startup code here
    }
 
Comment
Add comment · Show 6 · 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 23, 2018 at 11:02 AM 0
Share

I've tried it, it doesn't work.

avatar image JDelekto Reun_ · Jun 23, 2018 at 12:42 PM 0
Share

I had different result, but I did have to fill in the blanks. First, I created two scripts you don't have listed with dummy methods, for SetSpawner and Figure$$anonymous$$oves objects. If these were meant to be simple stand-alone objects, I was not able to set them in the editor. I'm not sure if the intent was to create an use them internally or if it was to attach them as scripts to a game object and then save them in the manager. Anyhow, in my Awake() function on the Game$$anonymous$$anager, I initialized SS and F$$anonymous$$ to a new instance of these objects:

 public class SetSpawner
 {
     public void SpawnNormal() { }
 }
 
 public class Figure$$anonymous$$oves
 {
     public void Input(GameObject[,] tiles) { }
 }
 
 // Inside Game$$anonymous$$anager:
     public void Awake()
     {
         this.SS = new SetSpawner();
         this.F$$anonymous$$ = new Figure$$anonymous$$oves();
     }

Now, from my assumptions, it looks like you have a game object called "Tiles" that you assign to the Tiles property of the Game$$anonymous$$anager, the "tiles" 2-dimensional array appears to be constructed of a hierarchy of game objects. "Tiles" is the parent game object with eight "Tile" children and each tile child has eight "Tile" children beneath them.

I modified the Start() method to assign the Game$$anonymous$$anager to each of the tiles as it processed them in the nested loops:

         for (int i = 0; i < 8; i++)
         {
             Transform parent = Tiles.transform.GetChild(i);
 
             parent.gameObject.GetComponent<Tile>().G$$anonymous$$ = this;
             for (int j = 0; j < 8; j++)
             {
                 tiles[i, j] = parent.GetChild(j).gameObject;
                 tiles[i, j].gameObject.GetComponent<Tile>().G$$anonymous$$ = this;
                 tiles[i, j].gameObject.GetComponent<Tile>().PosInput(i, j);
             }
         }

When running the game and clicking on one of my tiles (which I basically added a sprite renderer, a simple sprite and a 2D box collider to get the mouse down events), the output in the debug log was as follows: "TrueTrueTrueTrue"

Was this what you were expecting?

avatar image Reun_ JDelekto · Jun 23, 2018 at 02:39 PM 0
Share

The figuremoves and setspawner is both just a script which could be in the gamemanager script but they are long and use a lots of variable and I don't want them in the gamemanager. And the problem is not that I don't make them a value. If I put the code in the input method in to the start method the output is TrueTrueTrueTrue. So the problem is that after the Start method and before the Input method they just lose their value. Another question with your code why would I make the Tile Object be equal to the Gamemanager object?

Show more comments
Show more comments

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

100 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

Related Questions

Problem when trying to save variable in another variable. 1 Answer

[JS] Adding a scripts variable to a variable 1 Answer

is it better to use MeshFilter[] or three separate MeshFilter var's 1 Answer

comparing variables of gameobjects in an array 2 Answers

Access to components and their variables in Array. 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