- Home /
Script Not Working On Prefab
I seen some similar questions posted about this, but none of the ones I read were answered. I wrote a script that simulates a wind zone.
There is a collider on the wind zone game object that turns a bool "inwindzone" true when the player is colliding with it, false if not. When I have a pre-existing game object with the tag "Player" already in the hierarchy and pass the wind zone script the game object from the hierarchy , on game start the script works fine on the player. (Moves the player up for a period of time, pauses movement all together, moves the player down, then pauses) this repeats as long as the player is in the wind zone.
However when I delete the player from the hierarchy, add the player prefab to the wind zone script where it accepts the gameobject player, and have my spawn manager instantiate the player prefab in the scene on start, inside the wind zone, there is no movement. The wind zone does not effect the player prefab.
I thought collision detection might be an issue, but even if I move the instantiated player outside of the wind zone and move it back in the inwindzone bool is true, however nothing else is performed on it, or so it seems. The tag is still labelled player. I'm not sure why this isn't working, I'm still relatively new to unity and still learning!
My player script just contains the player movement so I didn't bother to post that, but I have attached my wind zone script and spawn manager script that instantiates the player below:
public class WindArea : MonoBehaviour {
public GameObject player;
public float strength;
public bool inWindZone = false;
public bool movingUp = true;
public bool movingDown = false;
public bool pauseUp = false;
public bool pauseDown = false;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
inWindZone = true;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
inWindZone = false;
}
}
void Update()
{
{
if (pauseUp)
{
StartCoroutine(DelayUp());
}
else if (pauseDown)
{
StartCoroutine(DelayDown());
}
else if (movingUp)
{
StartCoroutine(Moveup());
}
else if(movingDown)
{
StartCoroutine(Movedown());
}
}
}
IEnumerator Moveup()
{
if(inWindZone)
{
player.transform.Translate(new Vector2(0, 1) * strength * Time.deltaTime);
}
yield return new WaitForSeconds(3);
movingUp = false;
movingDown = false;
pauseDown = false;
pauseUp = true;
}
IEnumerator DelayUp()
{
yield return new WaitForSeconds(5);
movingUp = false;
movingDown = true;
pauseUp = false;
pauseDown = false;
}
IEnumerator Movedown()
{
if (inWindZone)
{
player.transform.Translate(new Vector2(0, -1) * strength * Time.deltaTime);
}
yield return new WaitForSeconds(3);
movingDown = false;
movingUp = false;
pauseUp = false;
pauseDown = true;
}
IEnumerator DelayDown()
{
yield return new WaitForSeconds(5);
movingDown = false;
movingUp = true;
pauseUp = false;
pauseDown = false;
}
}
public class SpawnManager : MonoBehaviour {
[SerializeField]
private GameObject _player;
// Use this for initialization
void Start () {
Instantiate(_player, new Vector2(0, -27), Quaternion.identity);
}
// Update is called once per frame
void Update () {
if (_player.transform.position.y <= -35)
{
StartCoroutine(Respawn());
}
}
IEnumerator Respawn()
{
Destroy(this.gameObject);
yield return new WaitForSeconds(3);
Instantiate(_player, new Vector2(0, -30), Quaternion.identity);
}
}
I also forgot to mention I'm not getting any errors or warnings on runtime.
Answer by LCStark · Oct 01, 2018 at 03:11 PM
Your WindArea
script is changing the position of the prefab, not the instanced object. If you change the transform of the prefab, it'll have no effect on already created instances. In your case, it won't have any effect at all - when you instantiate your object, you position it at Vector2(0, -27)
, so you ignore the prefab position.
In your SpawnManager
class you create an instance of the player object, but you don't store your reference anywhere. You have to either store a reference to your new instance in a field of that class and pass it to the wind area script, or use something like GameObject.Find
to find your object in the scene from the wind area script directly.
Answer by Courtneyssg · Oct 01, 2018 at 05:24 PM
Thanks, this was the issue. I didn't realize I had to create a reference to the instance. I created a public variable public Player player
and after my prefab gets instantiated I used FindObjectOfType<Player>()
. If player gets destroyed I instantiate it again and use FindObjectOfType<Player>()
in the Respawn Coroutine.
Yeah, that should work fine. If your public Player player
is in the Spawn$$anonymous$$anager
class, you don't even need to use FindObjectOfType
- you can simply use the return value of Instantiate
:
IEnumerator Respawn()
{
Destroy(this.gameObject);
yield return new WaitForSeconds(3);
GameObject playerGO = Instantiate(_player, new Vector2(0, -30), Quaternion.identity);
player = playerGO.GetComponent<Player>();
}
Your answer
Follow this Question
Related Questions
How to create GameObject (from prefab) at location of BoxCollider2D with rotation? 0 Answers
Instantiate Objects from text file 1 Answer
BoxCollider2D failling verification when Instantiate (Fixed, Cause: big derp) 1 Answer
Instantiate a prefab and then add a animator component and controller to the component. 2 Answers
Put a prefab in Hierarchy without using Instantiate ? (from code) 2 Answers