Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by $$anonymous$$ · Sep 11, 2014 at 07:40 PM · instantiateprefabdestroy

help for start my project...

I want to make an application in which some squares are generated at random positions and that they are destroyed when the user clicks on it (score increase) ...

How can i realize that? Actually I have a background with a script wich generate the square (prefabs)... I can't make the script that detect when a square is pressed... help me :(

 var enemy : GameObject;
 var timeDelay = 5;
  
 function Start() {
     while (true) {
         yield WaitForSeconds(timeDelay);
         Instantiate( enemy, Vector3(Random.Range(-4.5,4.5),Random.Range(-8.6,8.6),-0.1), Quaternion.identity );
     }
 }
 

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Erestyn · Sep 11, 2014 at 09:13 PM

You'll need something in your update function (something that runs once per frame) that acknowledges the player has clicked on a block, and increment the score by X points. You'd need to declare an int to store the users score.

Unityscript isn't my strong point but your code should look something like this.

  function Update() {
     if (Input.GetMouseButtonDown(0)) {
         enemy.renderer.enabled = false;
         playerScore++;
     }
  }

Notice that I disable the renderer rather than use 'destroy'. Once something is destroyed, it can't be instantiated as it was originally.

GetMouseButtonDown

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
avatar image
0

Answer by orb · Sep 11, 2014 at 07:59 PM

Attach a script with an OnMouseDown() method to the square objects, where you destroy it, like this:

 using UnityEngine;
 
 public class Obliterate : MonoBehaviour
 {
     void OnMouseDown()
     {
         Destroy(gameObject, 0.01f);
     }
 }
Comment
Add comment · Show 5 · 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
avatar image alexpizzini · Sep 11, 2014 at 08:02 PM 0
Share

Except, he's using "javascript", so the code he wants will be a touch different than the C#.

I'm curious as to why you put a .01 second delay on the destroy call.

avatar image $$anonymous$$ · Sep 11, 2014 at 08:06 PM 0
Share

Already done, but it doesn't work, but it delete all instance of prefabs...

I have just read in the forum that i have to use raycast but I can't understand how to use it

avatar image ExtremePowers · Sep 11, 2014 at 08:08 PM 0
Share

Here it is in java(unityscript), and the reason he might be putting in a delay is so the object has 0.01 seconds to destroy itself.

 #pragma strict
 
 function On$$anonymous$$ouseDown() {
     Destroy(gameObject);
 }
avatar image orb · Sep 11, 2014 at 08:13 PM 0
Share

A brief delay has been recommended when destroying stuff, so I went with that. It's quite possible it can be removed. Translating it to UnityScript shouldn't be hard, and it was only an example.

Prefab or not, it shouldn't be destroying every instance. Exactly how does the project layout look like?

avatar image wijesijp · Sep 11, 2014 at 08:17 PM 0
Share

you need to add the script to the enemy prefab not to the one that create enemies.

Also you need to have a collider on the enemy prefab to get the onmousedown work

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer

Active Prefabs 1 Answer

Accessing children of an instance doesn't work every time 1 Answer

how to instantiate object with content prefab and not him self 2 Answers

How do I destroy a Instantiated UI image that is pushed on the Canvas? 2 Answers


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