I’m a student taking a course that requires making a 2D game. One of the primary mechanics in my game is that new parts of the level appear based on the coin value. However, I’m very much a beginner at all aspects of this stuff. Right now, the first set of platforms start off as invisible, but can be activated by pressing F. How can I add something to the script (below, I just took it from a YouTube tutorial) that will check the coin count? Like ‘if coinValue == 2’ and then the F press will activate the next platform. Id like to keep it real simple that way if stuff breaks down the line I have some chance of fixing it myself. Thanks for your time!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Showhide : MonoBehaviour
{
public GameObject Sunflower;
void Update()
{
if(Input.GetKeyDown(KeyCode.F))
{
Sunflower.SetActive(true);
}
}
}
you can combine both criterias in one if statement, so it will only be activate if the player pressed f AND if he has 2 coins. But this will only work as long as your coinValue is 2, as soon as it is higher that part of the if will be false and it will not reach anymore the code inside of the if
That’s fine. I know this is a more tedious way to do it but I’m probably going to be copy/pasting the script to suit each area’s required coin value.
So to be sure I understand, I can just use the term “coinValue” and the script will know what that is? Or do I need to look at whatever script is tracking the coins to see exactly what term to use? (Like maybe it’s just “coin” or “score” or something else)
You want to avoid copy and pasting code, in case you need to go back and change it for example. What course is this, can you share the instructions? If you are not familiar with C# scripting, it might not be the correct course for you. And no, your script won’t automatically know coinValue, you would need to define and properly implement all your variables.
As already mentioned you have to define your variables in each script. If you wanna copy/pasty your code each time, you can just update the criterias. for example:
script/object1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Showhide : MonoBehaviour
{
public GameObject Sunflower;
[SerializeField] int coinValue; // use [SerializeField] if you wanna have a private variable which value can be changed in the editor otherwise just make it private/public
void Update()
{
if(Input.GetKeyDown(KeyCode.F) && coinValue == 2)
{
Sunflower.SetActive(true);
}
}
}
script/object2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Showhide : MonoBehaviour
{
public GameObject Sunflower;
[SerializeField] int coinValue; // use [SerializeField] if you wanna have a private variable which value can be changed in the editor otherwise just make it private/public
void Update()
{
if(Input.GetKeyDown(KeyCode.F) && coinValue == 4)
{
Sunflower.SetActive(true);
}
}
}
You need on any place of your code a line, which sets the cointValue if anything happens in the game (e.g. the player collected a new coin…)
I hope i understood correctly what you wanna do and it helps
There’s not really super specific instructions. Just have a player, an enemy that hurts you, a ui, collectibles, and platforms. It’s a beginner class so they supply the scripts and don’t expect you to code anything. I’m just trying to do something that’s not among those scripts.
I need these different areas to appear based on the score (1 point triggers platform A, 2 pts gets platform B, etc) and I can’t figure out how to tell this script what the player’s current score is. The player gets their score from picking up collectibles. That part works and the score displays correctly. So I want to tell the ‘platformAppear’ script what the score is but I’m unsure of how to do that.