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:
- cooldown between sushis spawned is decreased
- 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?
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.
and just add
newTarget.GetComponent<SushiController>().increasedGravity=true;to the instantiation code instead ofnewTarget.GetComponent<Rigidbody2D>().gravityScale += .3f;