I am creating a platform jumping game. I have a grid of platforms each with coin hovering above it for the player to jump to collect. When the player collects every coin in the first level, the game will reset, and add more platforms to increase the difficulty.
I am unsure of a couple things. The main thing is how to reset the world without using Application.LoadLevel. My Parent prefab, “parent1” contains two children: platform, and coin.
Here’s my worldscript which manages the world
CoinScript coinScript;
public GameObject platform;
public int xPlatforms;
public int yPlatforms;
public int spacing;
[HideInInspector]
public int numberOfPlatforms;
private void Awake()
{
coinScript = platform.GetComponent<CoinScript>();
if(coinScript == null)
{
Debug.Log("coinscript could not be found!");
}
}
private void Start()
{
if (spacing <= 0)
{
Debug.LogError("Can't have negative spacing value!");
}
else
{
InstantiatePlatforms();
}
}
private void Update()
{
}
//Create World
public void InstantiatePlatforms()
{
for (int x = 0; x < xPlatforms; x++)
{
for (int y = 0; y < yPlatforms; y++)
{
platform = Instantiate(platform, new Vector3(x * spacing, 0, y * spacing), Quaternion.identity);
platform.name = "Platform " + x + ", " + y;
platform.tag = "platform";
numberOfPlatforms++;
}
}
}
Here’s my CoinScript that returns null.
private void Start()
{
}
//If tag "Player" collides with this, subtract
//counter from GameScript
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
this.gameObject.SetActive(false);
GameObject.FindObjectOfType<GameScript>().counter--;
}
}
public void ResetCoins()
{
this.gameObject.SetActive(true);
}
What’s the best way to reset the world once the player collects all of the coins? No matter where I call resetCoins, it seems to return nullreferenceexception, even though OnTriggerEnter always works. I’ve tried calling the functions in the prefab, although I have learned it is better to call the functions once they’re instantiated in the game.
Its not clear what you are trying to achieve.
Do you want the next set of platforms to be added after all the coins in what you call your first stage is finished?
Or does the player start all the way back from the beginning of the first stage?
From your scripts I can only guess its supposed to be an endless “runner”, I’d make a further guess that its for mobile.
Also, you scripts don’t look like they should have any issue. If CoinScript.cs 's ResetCoins function is truly where the problem lies, it means the gameobject is missing, which should not be the case if you are referencing the instantiated object. If you are referencing the prefab however, I’ve never tried that and it might be the issue. It might be more helpful to see the actual error itself.
Somehow I am no longer getting the error caused(it was caused by the call, however the coins are not getting reset when a player falls out of position. Is there a place that I can upload my project to show?
By the way here’s the error:
NullReferenceException: Object reference not set to an instance of an object
GameScript.ResetWorld () (at Assets/Scripts/Game/GameScript.cs:43)
GameScript.Update () (at Assets/Scripts/Game/GameScript.cs:34)
Line 43 refers to calling ResetCoins function in CoinScript, and line 34 refers to ResetWorld call in update.
Well either player, cs or coinScript is null when you go to access it, simple as that. You need to make sure it’s finding the proper objects and figure out why it might not be as that’s the case. It’s possible those scripts/objects are created after the Awake() is called, so it’s not finding them and simply moving that logic to Start() instead will fix the issue.
Thanks for the advice, but none of the suggestions worked. CoinScript is null, but every other script call works. The Player GameObject is correctly assigned. I moved the FindObjectOfType script calls to Start() but that did not work either.
CoinScript and GameScript are in the same folder, so that should not be a problem.
It’s impossible to call a non-static method on an object that isnt there. You are most likely not referencing what you think you are referencing. I honestly cant be bothered to find out such an issue since linkages between your scripts are your burden to bear.
But let’s get back to the original issue, you want to know how to reset your coins. Your WorldScript.cs seems to act as a game manager already. What you can do is keep a list of all coins that are in the scene, and call the SetActive of all the coins in WorldScript.cs instead.
//include this list in WorldScript.cs
public List<GameObject> coins;
//register the coins from the platforms or the CoinScript itself
//in CoinScript for example
void Awake(){
GameObject.FindObjectOfType<WorldScript>().coins.Add(gameObject);
}
//in WorldScript.cs
//call this function when you want to reset all coins
void ResetCoins(){
foreach(CoinScript c in coins){
c.SetActive(true);
}
}
I think that approach is working, ResetCoins is being called, but SetActive is not resetting the coin to active once the player falls out of range. Playing with debug.log but not really coming up with much. Time to do a little more!