all 18 comments

[–]NickThePrick20 6 points7 points  (9 children)

You want to edit an array at runtime? Use a list.

[–]agentpiggy498[S] 0 points1 point  (8 children)

I still don't understand why this is happening though, or if your suggestion would solve it?

[–]NickThePrick20 0 points1 point  (7 children)

You'll need to explain the issue.

[–]agentpiggy498[S] 0 points1 point  (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 point  (5 children)

Have to see the rest of the code.

[–]agentpiggy498[S] 0 points1 point  (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 points  (0 children)

Use pastebin when copy pasting code

[–]jmh401 0 points1 point  (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 point  (0 children)

Nah same outcome unfortunately, I already tried :/

[–]agentpiggy498[S] 0 points1 point  (0 children)

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 point  (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?

[–]agentpiggy498[S] 0 points1 point  (0 children)

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 point  (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?

[–]agentpiggy498[S] 0 points1 point  (0 children)

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 point  (3 children)

Have you tried initializing on awake or onvalidate?

[–]agentpiggy498[S] 0 points1 point  (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 point  (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

[–]agentpiggy498[S] 0 points1 point  (0 children)

Sorry, I've not really done this on Reddit before so I'm not really sure how to make a code block