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 /
avatar image
0
Question by Pugatboss · May 29, 2016 at 07:26 PM · gameobjectmousedown

How to set a gameobject with a mouse click

Hey so I am an amateur coder in c# and I cannot seem to figure out how to answer this question. Here is the script I am working on so far.

 using UnityEngine;
 using System.Collections;
 
 public class Attack : MonoBehaviour 
 {
     public bool IsDead;
     public bool AddEXP;
     public bool CanUpgrade;
     public bool EnemyIsDead;
     public int EnemyHealth;
     public int MyHealth;
     public int Damage;
     public int EnemyDamage;
     public int exp;
     public int expToUpgrade;
     public int enemyToughness;
     public GameObject Enemy; // I want the player to be able to set this gameobject with the click of a mouse. HOW?
     public GameObject Me;
     void Start()
     {
         IsDead = false;
         EnemyHealth = 10;
         MyHealth = 20;
         Damage = 1;
         EnemyDamage = 1;
     }
     void Update()
     {
         if (MyHealth <= 0) {
             IsDead = true;
         } else if (MyHealth >= 1) 
         {
             IsDead = false;
         }
         if (EnemyHealth >= 1) {
             EnemyIsDead = false;
         } else if (EnemyHealth <= 0) 
         {
             EnemyIsDead = true;
         }
         if (EnemyIsDead == true) 
         {
             Destroy (Enemy.gameObject);
         }
         if(IsDead == true)
         {
             Destroy (Me.gameObject);
         }
         if (exp >= expToUpgrade) 
         {
             exp -= expToUpgrade;
             Damage += 5;
             enemyToughness += 1;
         }
         EnemyDamage = enemyToughness * 5;
         EnemyHealth = enemyToughness * 10; //Will change if gets out of hand
         if(EnemyIsDead == true && AddEXP == true)
         {
             exp += enemyToughness * 5;
             AddEXP = false;
         }
     }



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 Pugatboss · May 28, 2016 at 11:39 PM 0
Share

I just figured out a way to do it by just resetting the enemy health if it clicks another enemy but if anyone else knows anything it would be great for the future!

avatar image nathanvj · May 29, 2016 at 08:29 PM 0
Share

public GameObject enemy;

On the object that the Player can click to select the enemy attach a script and a collider.

Then in the script on the enemy you want to put something with an On$$anonymous$$ouseDown function.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by nixtwiz · May 29, 2016 at 09:16 PM

Combination of these two docs (I frequently refer back to them, always forget parts for some reason):

http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html http://docs.unity3d.com/ScriptReference/Physics.Raycast.html

The code you need:

 if (Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit rayHit;
             if (Physics.Raycast(ray, out rayHit, 100.0f)){
                 if(rayHit.collider.tag == "Enemy")
                 {
                     enemy = rayHit.collider.gameObject;
                 }
             }
         }

Change the 100.0f to the max distance you want to be able to click enemies. Just put a collider on your enemies and tag them "Enemy" (or change that to whatever). If you don't have the colliders on the parent you could use transform.parent to get to the parent object or something like that.

Comment
Add comment · Show 2 · 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 Pugatboss · May 29, 2016 at 10:24 PM 0
Share

Great thanks I will see if this works.

avatar image Pugatboss · May 29, 2016 at 10:29 PM 0
Share

Looks like it did! Thanks man.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

MouseDown Triggered by Another GameObject 1 Answer

How to set Minimum and Maximum angles on Drag to Rotate Gameobject ?? 0 Answers

Mouse drag with specific angle 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Unity UI - Data Management for GameObjects in ScrollRect/TableViews with Large Data Sets 0 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