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 /
avatar image
0
Question by jgratrix · Dec 27, 2015 at 04:51 PM · spawningspacespace shootermissileturrets

Detect if Game Object has been clicked on in 3Dspace

hey so ive been looking around and struggled to find anything on that fitted with what i needed. Anyway so im looking to click on a gameobject while in game then afterwards i want to use that as my target for my missile script however when i come to run it, its saying its not reference to the instance of the object... have i missed something out or does the way im finding the gameobject from the mouse click not like to be used later on in the script? thanks tones in advance here's my code so far:

 using UnityEngine;
 using System.Collections;
 
 public class turretplayer : MonoBehaviour {
 
 Transform enemy;
 public GameObject Missile;
 public float enemyDist;
 public GameObject spawn;
 bool Space = Input.GetKey(KeyCode.Space);
 bool fire;
 Rigidbody rb;
 
 void OnMouseDown()
 {
 enemy = gameObject.transform;
 
 }
 
 void Start() { rb = GetComponent(); }
 
 void Update () {
 
 //calc enemy dist
 enemyDist = Vector3.Distance(transform.position, enemy.position);
 //if enemy is close by
 if (enemyDist <= 100)
 {
 Quaternion neededRotation = Quaternion.LookRotation(transform.position - enemy.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, neededRotation, Time.deltaTime * 0.5f);
 if(Space == true)
 {
 Instantiate(Missile, spawn.transform.position, spawn.transform.rotation);
 rb.AddForce(transform.forward * 10F);
 }
 }
 }
 }
  

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by allenallenallen · Dec 27, 2015 at 04:59 PM

What you wrote:

 Transform enemy;

 void OnMouseDown()
 {
     enemy = gameObject.transform;
 }

What's enemy? Since you didn't declare the GameObject via script, you need to drag the "enemy" in the editor, which you didn't do. That's why you have the error. Basically, you need a reference to the "enemy" GameObject.

I'm done talking about the error but your approach is problematic. What were you trying to accomplish by setting the position of the enemy to this current GameObject's position when this GameObject is clicked? If you put that to practice, what would happen is that the player clicks on the GameObject this script is attached to and then the enemies will teleport to this GameObject. Are you sure this is what you want?

Comment
Add comment · Show 1 · 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 jgratrix · Dec 29, 2015 at 10:41 PM 0
Share

Oh no, not what i wanted, now looking back at my code i realize. What i originally wanted was for a large Bullet to be fired out by a turret on my player ship. and i wanted the turret to turn it self to face the enemy in the distance only if it was close to the player by 100 radius. I don't want the bullet to aim at the target i wanted it to simple fire out of the turret in a straight line. One problem i keep co$$anonymous$$g into is how to Instantiate the bullet and make it continue traveling for a maximum 10 seconds then if its traveling for more that that it would simple destroy its self.

Could you help me there in anyway?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need help with getting a script from a game object spawned in as child object of player object in multiplayer 0 Answers

explosion problem in space shooter 0 Answers

The bolt on "Shooting shots" won't appear 1 Answer

How do I complete homing missile movement? 0 Answers

Missle/Bullet script. C# 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