-1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Platform : MonoBehaviour
{
    public float speed = 10.0f;
    private Rigidbody2D rb;


    // Use this for initialization
    void Start()
    {
        rb = this.GetComponent<Rigidbody2D>();
        rb.velocity = new Vector2(-speed, 0);

    }

    // Update is called once per frame
    void Update()
    {
        //not important
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        //not important
    }
}

^^ This is my Platform.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformSpawner : MonoBehaviour
{
    public GameObject Platform;
    public GameObject Fireball;
    public float respawnTime = 0.1f;

    // Use this for initialization
    void Start()
    {
        StartCoroutine(ObjectSpawning());
    }
    private void spawnPlatform()
    {
//not important
    }
    private void spawnFireball()
    {
//not important
    }
    IEnumerator ObjectSpawning()
    {
        while (true)
        {
            yield return new WaitForSeconds(respawnTime);
            spawnPlatform();
            spawnFireball();
            respawnTime *= 1.02f;
        }
    }
}

^^ This is my PlatformSpawner.cs

I'm new to unity and C#, how can I change the value speed in Platform.cs from PlatformSpawner.cs ? I looked up online but I cant seem to find an answer... So I hope you guys can help me! I'm trying to gradually increase the speed value btw. Thanks in advance (:

Ilan1009
  • 7
  • 1
  • 6

1 Answers1

0

Do you want to change the speed for just one platform, or change for all platforms?

If you want the same speed for all platforms, you should make speed static.

public static float speed = 10.0f;

Then you can adjust the speed like this.

Platform.speed = 15.0f; //replace 15.0f with your desired speed.

If you want to change the speed for a specific platform, get the Platform component from a game object, and modify speed.

// assuming "platform" is of type gameObject
platform.GetComponent<Platform>().speed = 15.0f; // replace 15.0f with your desired speed.
Dharman
  • 30,962
  • 25
  • 85
  • 135
rohan
  • 124
  • 3
  • 9