How do I change the gravity of a prefab?

52 Views Asked by At

so basically I have a TargetSpawner which is simply a BoxCollider in the shape of a rectangle put on top of the screen which spawns little sushis (TargetPrefab) to shoot.

The little sushis are prefabs with their own RigidBody2D.

the idea is that after 10 sushi (TargetPrefab) spawned, the game difficulty increases in this way:

  1. cooldown between sushis spawned is decreased
  2. the gravity scale at which the sushis increases.

I'm doing this inside the TargetSpawner's script. no problems with the cooldown at all since I'm operating INSIDE the TargetSpawner's script, but I don't know how to recall the rigidbody of the sushi (TargetPrefab) from within this script.

`{        
    [SerializeField] private BoxCollider2D cd;
    [SerializeField] private GameObject targetPrefab;
    [SerializeField] private float cooldown;
    private float timer;
    
    public int sushiCreated;
    private int sushiMilestone = 10;
       
    void Update()
    {        
        timer -= Time.deltaTime;

        if (timer < 0)
        {
            timer = cooldown;                      
            
            sushiCreated++;

            GameObject newTarget = Instantiate(targetPrefab);

            float randomX = Random.Range(cd.bounds.min.x, cd.bounds.max.x);

            newTarget.transform.position = new Vector2(randomX, transform.position.y);

            if (sushiCreated > sushiMilestone && cooldown > .5f)
            {
                sushiMilestone += 10;
                cooldown -= .3f;
                newTarget.GetComponent<Rigidbody2D>().gravityScale += .3f;
                             
            }
                        

        }
    }
}`

My attempt is the last line of code "newTarget.GetComponent().gravityScale += .3f;" but no results so far.

I've tried to look in already posted questions but no results either.

What can I do to recall the Rigidbody of the Prefab and interact with it?

2

There are 2 best solutions below

1
NEPTTUNE On

I would try checking that attempting to change the gravityScale to a larger number actually works (like making it 10 for example), and if it does, then it may be that 0.3 extra is not noticable.

If it doesn't work (I have no idea why it wouldn't), I would probably abandon gravityScale altogether and, inside a script on the object you are instantiating, have a bool for the increased gravity and if it is enabled (which you can do when you instantiate it), add extra force (which would have 0 for the x value and 0.3 × the gravity strength (presumably -9.81) × Time.deltaTime for the y value) every update to the object which would simulate the increased gravity

e.g.

using UnityEngine;

public class SushiController : MonoBehaviour
{
    public bool increasedGravity=false;

    void Update()
    {
        if(increasedGravity)
        {
            this.gameObject.GetComponent<Rigidbody2D>().AddForce(Physics2D.gravity*0.3f*Time.deltaTime);
        }
    }
}

and just add newTarget.GetComponent<SushiController>().increasedGravity=true; to the instantiation code instead of newTarget.GetComponent<Rigidbody2D>().gravityScale += .3f;

1
derHugo On

You are doing the

 newTarget.GetComponent<Rigidbody2D>().gravityScale += .3f;

only once for each new created object.

You probably want to rather keep track of how much you actually need to decrease - which is more with every milestone:

// use the correct type right away
[SerializeField] private Rigidbody2D targetPrefab;

void Update()
{        
    timer -= Time.deltaTime;

    if (timer < 0)
    {
        timer = cooldown;                      
        
        sushiCreated++;

        var newTarget = Instantiate(targetPrefab);

        var randomX = Random.Range(cd.bounds.min.x, cd.bounds.max.x);

        newTarget.position = new Vector2(randomX, newTarget.position.y);

        // integer division
        // 0-9 => 0
        // 10-19 => 1
        // 20-29 => 2
        // ...
        var milestone = sushiCreated / 10;

        if (milestone > 0 && cooldown > .5f)
        {
            cooldown -= .3f;

            // 0-9 => not called at all
            // 10-19 => += 0.3
            // 20-29 => += 0.6
            // ...
            newTarget.gravityScale += 0.3f * milestone;
              
        }
    }
}

If your question however is how to also increase the gravityScale of all already existing objects - then you would either store them in a list and iterate over them, or rather directly adjust the global Physics2D.gravity