- Home /
I want to access objects in my scene with a prefab when it spawns.
I have a rocket that follows the player around. I made a spawner script that spawns the rockets with equal intervals. I am running into a problem and that it that, the rockets do spawn, but they can't access player or look for the player in the scene. Any idea on how to fix this? Also, my game is one of those endless rocket dodging games.
here is the script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))] public class MissileBehaviour : MonoBehaviour {
public GameObject explosionVFX;
public Transform target;
public float speed = 5f;
public float rotateSpeed = 200f;
private Rigidbody2D rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Vector2 direction = (Vector2)target.position - rb.position; //getting direction
direction.Normalize();
float rotateAmount = Vector3.Cross(direction, transform.up).z;
rb.angularVelocity = -rotateAmount * rotateSpeed; //angular velocity
rb.velocity = transform.up * speed; //adding velocity based on global axis
}
void OnTriggerEnter2D()
{
GameObject explosionObject = Instantiate(explosionVFX, transform.position, Quaternion.identity);
Destroy(gameObject);
Destroy(explosionObject, .5f);
}
}
Answer by wfin · Dec 12, 2020 at 08:23 PM
Never mind. I solved it. Had to search around for a bit.
Your answer
![](https://koobas.hobune.stream/wayback/20220613021535im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to have my Editor Script save the changed settings when I hit Play 0 Answers
Unity project referencing another Unity project? 0 Answers
Prefab Brush from 2d-extras not working in tilemap 7 Answers
Door Missing 1 Answer
If I make changes to a prefab in the Project tab, do I have to save the scene, too? 1 Answer