Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Feb 15, 2018 at 01:15 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by plumel · Feb 09, 2016 at 03:23 PM · spawnrandom.rangegenerationontriggerenter2dspawnpoint

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image plumel · Feb 09, 2016 at 04:01 PM 0
Share

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!.

avatar image NoseKills · Feb 09, 2016 at 06:43 PM 0
Share

'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);

1 Reply

  • Sort: 
avatar image
0
Best Answer

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".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

41 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges