- Home /
Platform cannot move up
I am making a 2D game where you have to drop down platforms and avoid being brought up to a certain point. Unfortunately, I cannot game the platforms to move up. I have tried both with Vector3.MoveTowards and rb.velocity, the latter of which I did not choose because I need the platforms to be static. With the MoveTowards method, it still doesn't work. Here are the scripts:
//START SPAWNING PLATFORMS
void Start()
{
StartCoroutine(InstantiatePlatforms());
}
void Update()
{
//PLATFORM MOVEMENT
platform.GoUp(upSpeed);
}
// SPAWN PLATFORMS
public IEnumerator InstantiatePlatforms()
{
GameObject spawnedObject = platforms[Random.Range(0, platforms.Length)];
Vector3 spawnPosition = new Vector3(Random.Range(-1, 3), -10, 0);
Instantiate(spawnedObject, spawnPosition, Quaternion.identity);
yield return new WaitForSeconds(1);
StartCoroutine(InstantiatePlatforms());
}
void IncreaseDifficulty()
{
upSpeed++;
}
and the script for the platforms:
public void GoUp(float upSpeed)
{
transform.position = Vector3.MoveTowards(transform.position, new Vector3(transform.position.x, 8, transform.position.z), Time.deltaTime * upSpeed);
}
I have never worked with 2D and am wondering where I messed up. Thank you in advance!
BTW I set upSpeed to 10 and assigned every public variable.
Answer by xxmariofer · Dec 02, 2020 at 11:34 AM
You need to try and debug it yourself, unity is for sure giving you a null reference exception. your issue is that you never assign platform in your script
Simply move the GoUp script from the platforms class to the Update and remove the Update from the spawingn platforms script
Answer by OatMilkLover · Dec 02, 2020 at 12:56 PM
@xxmariofer Thank you for your help but it still does not seem to work. Also could you please elaborate on the second part? Do I move the code from the GoUp method into the spawner's Update? Sorry if I am just dumb and this is a really simple problem.
Edit: I get the second part of your post but I would like to be able to increase the difficulty via increasing the speed at which the platforms move up. Is there anyway I can do this with your solution? Thank you for your help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613020932im_/https://answers.unity.com/themes/thub/images/avi.jpg)