The question is answered, right answer was accepted
Generating game object in random position
Hi, I'm currently trying to get a gem to be picked up, dissappear, and respawn somewhere else random on the board. The game is 2D.
I'm having trouble with the game saying "UnassignedReferenceException: The variable Gem of PlayerMovement has not been assigned."
Here is my code
using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed;
public GameObject Gem;
Vector2 RandomSpawn = new Vector2 (Random.Range(-4.5F,4.5F), Random.Range(-9.5F,9.5F));
private Rigidbody2D rb2d;
void Start ()
{
rb2d = GetComponent<Rigidbody2D> ();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal"); //Gets keys that unity refers to being able to move horizontal (Set by default)
float moveVertical = Input.GetAxis ("Vertical"); //Gets keys that unity refers to being able to move vertical (Set by default) Use forces to act with RigidBody2D
Vector2 movement = new Vector2 (moveHorizontal, moveVertical);
rb2d.AddForce (movement * speed);
}
void OnTriggerEnter2D(Collider2D other)
{
StartCoroutine ("RespawnGem");
}
IEnumerator RespawnGem()
{
if (gameObject.CompareTag ("pickup"))
Destroy (Gem.gameObject);
Instantiate (Gem, RandomSpawn, Quaternion.identity);
yield return null;
}
}
I've only started doing this recently, anything is appreciated.
Thanks.
I've switched to a different method which is easier for me to see whats going on.
I've also moved the section from the player movement script to a seperate script i have put in the "Gem" game object prefab.
public class RandomSpawn : $$anonymous$$onoBehaviour {
public GameObject Gem;
void SpawnGem()
{
Vector3 RandomSpawn = new Vector3 (Random.Range (-4.5F, 4.5F), Random.Range (-9.5F, 9.5F),0F);
Instantiate (Gem, RandomSpawn, Quaternion.identity);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("pickup"))
SpawnGem ();
Destroy (Gem.gameObject);
}
}
Now, when the player moves over the gem, it disappears and is removed from the hierarchy but is not replaced. A warning at the bottom of unity says "The reference script on this Behavior (Game Object 'Gem') is missing!.
'Gem' is the prefab you are making copies of when you call 'Instantiate'. You shouldn.t try to destroy it. Ins$$anonymous$$d you should destroy the copy you make.
public GameObject Gem;
public GameObject spawnedGem;
...
spawnedGem = Instantiate(Gem...
...
Destroy(spawnedGem);
Answer by Firedan1176 · Feb 09, 2016 at 08:00 PM
Your Gem
GameObject should be a public reference from your scene or your project. If you are instantiating Gem
(which you are), you need to make sure that it is referenced in the Inspector. So under your script in the Inspector, make sure there is a GameObject in the field that says "Gem".
Follow this Question
Related Questions
continously spanws and overlaps 0 Answers
Random Object Spawn 0 Answers
Object spawn at spawn points 1 Answer
Spawn unique objects from the array 1 Answer
Generate Random Numbers a Distance Apart From Each Other 1 Answer