- Home /
Instantiated Game Objects' Script Won't Update
I am currently developing my first game (falling platformer) and I've got an object pool of platforms that randomly spawn as the character falls. I have set the game up so the camera remains static while the platforms move up. In an attempt to create the illusion of falling, I have set up an invisible collider about halfway down the screen so that when the player lands on it, the velocity of the platforms should increase. However, the velocity variable in my platform behaviour script attached to my platform prefabs will not update.
Here is my platform behaviour script which is attached to my platform prefabs as well as my platform generator game object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformBehaviour : MonoBehaviour
{
public float riseSpeed;
Rigidbody2D rb;
public GameObject fallCollider;
FallCollider colliderScript;
// Start is called before the first frame update
void Start()
{
riseSpeed = 2;
rb = GetComponent<Rigidbody2D>();
colliderScript = fallCollider.GetComponent<FallCollider>();
}
// Update is called once per frame
void Update()
{
Rise();
if (colliderScript.playerTouching == true)
{
riseSpeed = 4;
} else
{
riseSpeed = 2;
}
}
private void Rise()
{
rb.velocity = new Vector2(0, riseSpeed);
}
}
And here is my collider script which is attached to the invisible collider:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallCollider : MonoBehaviour
{
public bool playerTouching;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
playerTouching = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
playerTouching = false;
}
}
}
When I run the game and pause it when the player is on the collider, I can see the riseSpeed variable updates for the PlatformGenerator object but does not update for each of the instantiated platforms. Any guidance would be extremely appreciated as I have been racking my head over this for the last few days.
Use GameObject.Find() in the start() method of your PlatformBehaviour script to assign the fallCollider to your instantiated object. @$$anonymous$$ystical$$anonymous$$rCool
Answer by Shameness · Nov 20, 2020 at 11:15 AM
Since you never initialized fallCollider field in PlatformBehaviour:
Your generated platforms don't have reference to fallCollider.
On the other hand, your generetor object have reference to fallCollider because I guess you set it manually using editor.
Yes you're right I was trying to make the reference in the editor which wasn't working. After making the reference using FindWithTag() it worked perfectly. Thanks for your answer :)
Your answer
Follow this Question
Related Questions
Problems about prefabs and velocity affected by scripts 0 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
I made a rocket! Help! 2 Answers
Velocity from Rigidbody and from Transform don't match for small dt 1 Answer
Move Particles with Object but in world space (character controller) 0 Answers