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 MonsieurBate · Jun 10, 2019 at 10:43 AM · raycastgamecursormovingrts

Unity3D - RTS Game - Instantiate object on buttonclick and make it follow the cursors position

I am trying to instantiate an object of my "ObjectManager"-class by clicking on a button and then to make it follow my cursors position. Would like to know what I am doing wrong in this following code. I appreciate your support.


So, when the selectedObj is selected I want it to move it along my cursors positon. It is working great but expect of instantiated objects. Part of my SelectionManager script:

  void Update()
     {
         Ray currentCursorPos = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hitInfo;
 
         if (Physics.Raycast(currentCursorPos, out hitInfo, ignoreLayer))
         {
             objThatIsHit = hitInfo.collider.GetComponent<ObjectManager>();
 
             if (selectedObj == null || selectedObj.inSelection == false)
             {
                 selectedObj = (objThatIsHit.gameObject.tag == "obstacle" || objThatIsHit.gameObject.tag == "field") ? objThatIsHit : selectedObj;
 
                 OnSelect(); // Turns "inSelection" in true when Input.GetMouseButtonDown(0) == true
             }
             else if (selectedObj != null)
             {
                 selectedObj.transform.position = (selectedObj != objThatIsHit) ? hitInfo.point : selectedObj.transform.position;
 
                 OnDeselect(); // Turns "inSelection" in false when Input.GetMouseButtonDown(0 or 1) == true
             }
         }
     }

Below you can find the "objToSelect" variable which is temporarily made to obtain the instantiated object. After that I push this object to my "selectedObj" of my SelectionManager script and turn it's "inSelection" in true so it follows my cursor. But it's not working. The instantiated object just stops on a position next to the button I pressed but it's not moving until I click on it.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class UnitMenu : MonoBehaviour
 {
     [SerializeField] private GameObject[] units = null;
     [SerializeField] private Texture2D[] unitIcon = null;
 
     [SerializeField] private GameObject ghost3dTemplate;
 
     private SelectionManager selectionManager;
     private Vector3 objThatIsHit;
 
     private void Start()
     {
         selectionManager = GameObject.Find("SelectionManager").GetComponent<SelectionManager>();
     }
 
 
     void OnGUI()
     {
         CreateUnitUI();
     }
 
     private void CreateUnitUI()
     {
         int offset = 165;
 
         GUI.Box(new Rect(Screen.width / 2 - 250, Screen.height - 125, 500, 100), "");
 
         for (int i = 0; i < units.Length; i++)
         {
             GUIStyle icon = new GUIStyle();
             icon.normal.background = unitIcon[i];
 
             if (GUI.Button(new Rect(Screen.width / 2 - 245 + (offset * i), Screen.height - 120, 160, 90), units[i].name, icon) && CheckSelection())
             {
                 // Here is my issue with the function I want - First I instantiate the object, then give it to selectedObj and turn it's inSelection value in true
                 GameObject objToSelect = Instantiate(ghost3dTemplate, new Vector3(0,1,0), Quaternion.identity) as GameObject;
                 selectionManager.selectedObj = objToSelect.GetComponent<ObjectManager>();
                 selectionManager.selectedObj.inSelection = true;
             }
         }
     }

     // Is checking if an object is already selected - if an object is selected the buttons aren't possible to be pressed
     private bool CheckSelection()
     {
         if (selectionManager.selectedObj == null || selectionManager.selectedObj.inSelection == false)
         {
             return true;
         }
 
         return false;
     }
 }

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
Best Answer

Answer by MonsieurBate · Jun 10, 2019 at 04:02 PM

Solved it by myself. I use an ObjectManager script to get informations about my objects. I told the game to give each object when it's instantiated to be not in selection. It wasn't necessary so I removed it and it's working great. Sometimes it is really funny to see what a small code snippet can do with your game :D


 private void Start()
     {
         currentMaterial = GetComponent<Renderer>().material;
         originMaterial = currentMaterial;

         inSelection = false; // This wasn't necessary and it made the problem
     }

EDIT: I changed something to avoid issues when instantiating objects. I changed in the ObjectManager script the "Start()" function to "Awake()".

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 taoria123 · Jun 10, 2019 at 11:05 AM

is the OnDeselect triggered when you instantiate the object?

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 MonsieurBate · Jun 10, 2019 at 12:38 PM 0
Share

It should be because when it's instantiated then "selectedObj" isn't null anymore.

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

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

Related Questions

RTS Game: Unit selection through Raycast 2 Answers

Physics.Raycast not working as expected! 1 Answer

Having a player object move on coordinates 1 Answer

Non uniform movement radius 1 Answer

Spawn referencable objects with RayCast 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