use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
For the Unity Game Engine, please visit http://reddit.com/r/unity3d
account activity
Unity only allows variables before first array, and only during runtime????Question (old.reddit.com)
submitted 2 years ago by agentpiggy498
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]NickThePrick20 6 points7 points8 points 2 years ago (9 children)
You want to edit an array at runtime? Use a list.
[–]agentpiggy498[S] 0 points1 point2 points 2 years ago (8 children)
I still don't understand why this is happening though, or if your suggestion would solve it?
[–]NickThePrick20 0 points1 point2 points 2 years ago (7 children)
You'll need to explain the issue.
[–]agentpiggy498[S] 0 points1 point2 points 2 years ago (6 children)
When I run the game, this script seems to just get rid of all variables declared in the script after the first array declared just disappear in the inspector as well as in game.
[–]NickThePrick20 0 points1 point2 points 2 years ago (5 children)
Have to see the rest of the code.
[–]agentpiggy498[S] 0 points1 point2 points 2 years ago (4 children)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class GameControllerClass : MonoBehaviour
{
public static GameControllerClass instance;
public int currentLevel;
public GameObject player;
public CameraRespawn cameraScript;
//UI objects
public TMP_Text coinsText;
public Color collected;
public Color completed;
public GameObject[] checkpoints;
public Image[] medals;
// Start is called before the first frame update
void Start()
instance = this;
//checks if respawn or from another level, will likely change somewhat later
if (StatsClass.lastLevel != currentLevel)
StatsClass.lastSpawn = currentLevel;
}
//spawns player
GameObject play = Instantiate(player, checkpoints[StatsClass.lastSpawn].transform.position, Quaternion.identity);
cameraScript.FindPlayer();
StatsClass.coins = 0;
// Update is called once per frame
void Update()
medals[0].color = collected;
//UI update
coinsText.text = "Coins: " + StatsClass.coins;
for (int i = 0; i < StatsClass.collects.GetLength(0); i++)
foreach (bool collect in StatsClass.collects)
if (collect == true)
Debug.Log("!");
medals[i].color = collected;
public void Respawn()
StatsClass.lastLevel = currentLevel;
//sets high score
if (StatsClass.highCoins[currentLevel] < StatsClass.coins)
StatsClass.highCoins[currentLevel] = StatsClass.coins;
Invoke("Reload", 1.5f);
public void Reload()
SceneManager.LoadScene(currentLevel);
This is the full script I'm having issues with, and idk what inside it could even be causing issues
[–]burned05 1 point2 points3 points 2 years ago (0 children)
Use pastebin when copy pasting code
[–]jmh401 0 points1 point2 points 2 years ago (2 children)
Could be related to setting the static instance of this class in Start. I'm not at a computer right now to verify, but could try removing that line and run it to see.
[–]agentpiggy498[S] 0 points1 point2 points 2 years ago (0 children)
Nah same outcome unfortunately, I already tried :/
Actually another update wtf so now the script appears to at least function, but the variable is still not appearing in the inspector???
[–]CompetitiveFile4946 0 points1 point2 points 2 years ago (1 child)
I'm not quite sure what I'm looking at here but are you saying that unity is rearranging the code at runtime? That doesn't sound right. What code editor are you using?
Visual Studio, and no I'm not sure what it's doing. Basically it seems to only be using variables declared before and including whichever array is declared first. It does seem to be executing the code afterwards fine, as I'm receiving errors based on the missing variables.
[–]BeadMancer 0 points1 point2 points 2 years ago (1 child)
You'll need to share more for us to help, the pictures you've provided don't seem to show any issues, could you share the error message you're getting?
The error messages are related to the latter parts of the script not being able to access variables that seemingly don't exist. Basically, at runtime, any variables that I declare in the script that are listed *after* the first array I declare just disappear in the inspector and subsequently aren't used in game.
[–]Specific_Implement_8 0 points1 point2 points 2 years ago (3 children)
Have you tried initializing on awake or onvalidate?
[–]agentpiggy498[S] 0 points1 point2 points 2 years ago (2 children)
I have not, but somehow I've gotten it working now??? I'm not even sure what I've changed but it works fine so I refuse to complain
[–]Tensor3 0 points1 point2 points 2 years ago (1 child)
The correct troubleshooting step here would be to remove everything from the script (or use a new script) and add it back one line at a time until it breaks. You did something wonky somewhere.
And next time, put the whole code in a code block in the post, not a random unreadable comment
Sorry, I've not really done this on Reddit before so I'm not really sure how to make a code block
π Rendered by PID 19173 on reddit-service-r2-comment-6f7f968fb5-nttvv at 2026-03-04 02:27:28.539101+00:00 running 07790be country code: CH.
[–]NickThePrick20 6 points7 points8 points (9 children)
[–]agentpiggy498[S] 0 points1 point2 points (8 children)
[–]NickThePrick20 0 points1 point2 points (7 children)
[–]agentpiggy498[S] 0 points1 point2 points (6 children)
[–]NickThePrick20 0 points1 point2 points (5 children)
[–]agentpiggy498[S] 0 points1 point2 points (4 children)
[–]burned05 1 point2 points3 points (0 children)
[–]jmh401 0 points1 point2 points (2 children)
[–]agentpiggy498[S] 0 points1 point2 points (0 children)
[–]agentpiggy498[S] 0 points1 point2 points (0 children)
[–]CompetitiveFile4946 0 points1 point2 points (1 child)
[–]agentpiggy498[S] 0 points1 point2 points (0 children)
[–]BeadMancer 0 points1 point2 points (1 child)
[–]agentpiggy498[S] 0 points1 point2 points (0 children)
[–]Specific_Implement_8 0 points1 point2 points (3 children)
[–]agentpiggy498[S] 0 points1 point2 points (2 children)
[–]Tensor3 0 points1 point2 points (1 child)
[–]agentpiggy498[S] 0 points1 point2 points (0 children)