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 A1ec · Feb 10, 2018 at 09:39 PM · unity 5gameobjectobjectsclickclick objects

How to control an object after clicking on it?

I have a cannon i want to control (control the angle and shoot it) but not when i start the scene. Only after right clicking on it. I tried it like this:

 public class Shoot : MonoBehaviour
 {
 
     private float angle;
     public GameObject Cannon;
     public Text angleText;
 
     // Update is called once per frame
     public Rigidbody projectile;
     public float horizontalSpeed = 2.0F;
 
     public void Update()
     {
         if (Input.GetMouseButtonDown(1))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit, 100, 0))
             {
                 ControlCannon();
             }
         }
     }
 
 
     public void ControlCannon()
     {
         
                 float h = horizontalSpeed * Input.GetAxis("Mouse Y");
                 transform.Rotate(h, 0, 0);
                 Text();
                 if (Input.GetButtonDown("Fire1"))
                 {
                     Rigidbody clone;
                     clone = Instantiate(projectile, GameObject.Find("Firepoint").transform.position, transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.up * 10);
                 }
     }
     
 
     void Text()
     {
         angle = Cannon.transform.rotation.eulerAngles.x - 270;
         angleText.text = "Angle: " + angle.ToString();
     }
 }

But it doesn't do anything when i click on the cannon. Is there a different way or am i doing something wrong here?

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
0

Answer by Nomenokes · Feb 11, 2018 at 04:40 AM

Right now ControlCannon is only being called in the one tick you first click on it. If you look at line 21, it is nested within Input.GetMouseButtonDown and only there. What you should do instead is something like this:

 public GameObject cannon; //it's good practice to have variables lowercase
 //...
 public void Update()
      {
          ControlCannon(); //called every tick 
          if (Input.GetMouseButtonDown(1))
          {
              RaycastHit hit;
              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
              if (Physics.Raycast(ray, out hit, 100, 0))
              {
                  cannon = hit.collider.gameObject; //you assign the cannon when it is clicked
              }
          }
      }
 public void ControlCannon()
      {
          
                  float h = horizontalSpeed * Input.GetAxis("Mouse Y");
                  cannon.transform.Rotate(h, 0, 0); //controls the cannon
                  if (Input.GetButtonDown("Fire1"))
                  {
                      Rigidbody clone;
                      clone = Instantiate(projectile, cannon.transform.position, transform.rotation) as Rigidbody;
      //...




Comment
Add comment · Show 3 · 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 A1ec · Feb 11, 2018 at 03:06 PM 0
Share

I now get: "NullReferenceException: Object reference not set to an instance of an object" for the cannon variable in the ControlCannon(). I can't figure out how to put it in the right order to make it work.

avatar image Nomenokes A1ec · Feb 11, 2018 at 03:33 PM 0
Share

Yeah my code was a little incomplete. cannon will be null until it gets assigned. Do a check for null:

  public void ControlCannon()
  {
        if(cannon==null)return;
avatar image A1ec Nomenokes · Feb 11, 2018 at 10:09 PM 1
Share

It worked! Thank you so much, i knew it had to be something like that.

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

179 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 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 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 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 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

How to Setactive(true) the other class ? 0 Answers

Can I export a 3D gameObject made in unity as an fbx file to edit in blender? 1 Answer

Disable input while falling? 2 Answers

Unity 3d rotate gameobject in touch direction over an axis 0 Answers

How do I spawn allies properly? 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