Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Instantiated object not following mouse position

Discussion in 'Scripting' started by Sajid, Nov 17, 2015.

  1. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Hey all,

    I have an inventory script as seen here:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Inventory : MonoBehaviour {
    6.     [SerializeField]
    7.     private bool isPlacingObject;
    8.     [SerializeField]
    9.     private invItem[] invItems = new invItem[10];
    10.     [SerializeField]
    11.     private Image[] invHudImages = new Image[10];
    12.     [SerializeField]
    13.     private GameObject currentItem;
    14.     private RaycastHit rayHit;
    15.  
    16.     void Update()
    17.     {
    18.         if(isPlacingObject)
    19.         {
    20.             Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayHit);
    21.             Vector3 rayPosition = rayHit.point;
    22.             currentItem.transform.position = rayPosition;
    23.  
    24.             if (Input.GetButtonDown("Fire1"))
    25.             {
    26.                 isPlacingObject = false;
    27.                 currentItem = null;
    28.             }
    29.         }
    30.     }
    31.     public void addInvItem(invItem inItem)
    32.     {
    33.         invHudImages[getEmptyIndex()].GetComponentInChildren<Image>().sprite = inItem.hudSprite;
    34.         invItems[getEmptyIndex()] = inItem;
    35.        
    36.     }
    37.  
    38.     private int getEmptyIndex()
    39.     {
    40.         for(int i = 0; i <= invItems.Length; i++)
    41.         {
    42.             if(invItems[i].itemName == "")
    43.             {
    44.                 return i;
    45.             }
    46.         }
    47.         return -1;
    48.     }
    49.  
    50.     public bool isUnique(string inName)
    51.     {
    52.         if(invItems[0].itemName == "")
    53.         {
    54.             return true;
    55.         }
    56.  
    57.         for(int i = 0; i <= invItems.Length; i++)
    58.         {
    59.             if( invItems[i].itemName == inName)
    60.             {
    61.                 return false;
    62.             }
    63.         }
    64.         return true;
    65.     }
    66.  
    67.     private void instantiateAtMousePosition(GameObject objectToInstantiate)
    68.     {
    69.         Instantiate(objectToInstantiate, Camera.main.ScreenToWorldPoint(Input.mousePosition), transform.rotation);
    70.         isPlacingObject = true;
    71.     }
    72.  
    73.     public void item1()
    74.     {
    75.         if(isPlacingObject == false)
    76.         {
    77.             if (invItems[0].isDraggable)
    78.             {
    79.                 currentItem = invItems[0].dispModel;
    80.                 instantiateAtMousePosition(currentItem);
    81.             }
    82.         }
    83.     }
    84.  
    85.     public void item2()
    86.     {
    87.         if (isPlacingObject == false)
    88.         {
    89.             if (invItems[0].isDraggable)
    90.             {
    91.                 currentItem = invItems[0].dispModel;
    92.                 instantiateAtMousePosition(currentItem);
    93.             }
    94.         }
    95.  
    96.     }
    97.     public void item3()
    98.     {
    99.         if (isPlacingObject == false)
    100.         {
    101.             if (invItems[0].isDraggable)
    102.             {
    103.                 currentItem = invItems[0].dispModel;
    104.                 instantiateAtMousePosition(currentItem);
    105.             }
    106.         }
    107.  
    108.     }
    109.     public void item4()
    110.     {
    111.         if (isPlacingObject == false)
    112.         {
    113.             if (invItems[0].isDraggable)
    114.             {
    115.                 currentItem = invItems[0].dispModel;
    116.                 instantiateAtMousePosition(currentItem);
    117.             }
    118.         }
    119.  
    120.     }
    121.     public void item5()
    122.     {
    123.         if (isPlacingObject == false)
    124.         {
    125.             if (invItems[0].isDraggable)
    126.             {
    127.                 currentItem = invItems[0].dispModel;
    128.                 instantiateAtMousePosition(currentItem);
    129.             }
    130.         }
    131.  
    132.     }
    133.     public void item6()
    134.     {
    135.         if (isPlacingObject == false)
    136.         {
    137.             if (invItems[0].isDraggable)
    138.             {
    139.                 currentItem = invItems[0].dispModel;
    140.                 instantiateAtMousePosition(currentItem);
    141.             }
    142.         }
    143.  
    144.     }
    145.     public void item7()
    146.     {
    147.         if (isPlacingObject == false)
    148.         {
    149.             if (invItems[0].isDraggable)
    150.             {
    151.                 currentItem = invItems[0].dispModel;
    152.                 instantiateAtMousePosition(currentItem);
    153.             }
    154.         }
    155.  
    156.     }
    157.     public void item8()
    158.     {
    159.         if (isPlacingObject == false)
    160.         {
    161.             if (invItems[0].isDraggable)
    162.             {
    163.                 currentItem = invItems[0].dispModel;
    164.                 instantiateAtMousePosition(currentItem);
    165.             }
    166.         }
    167.  
    168.     }
    169.     public void item9()
    170.     {
    171.         if (isPlacingObject == false)
    172.         {
    173.             if (invItems[0].isDraggable)
    174.             {
    175.                 currentItem = invItems[0].dispModel;
    176.                 instantiateAtMousePosition(currentItem);
    177.             }
    178.         }
    179.  
    180.     }
    181.     public void item10()
    182.     {
    183.         if (isPlacingObject == false)
    184.         {
    185.             if (invItems[0].isDraggable)
    186.             {
    187.                 currentItem = invItems[0].dispModel;
    188.                 instantiateAtMousePosition(currentItem);
    189.             }
    190.         }
    191.  
    192.     }
    193.  
    194.  
    195. }
    196.  
    when item1() is called, it is supposed to instantiate an object (it does that fine), then move it to the current mouse position (it doesn't do that), and then make it stay under the cursor until isPlacingObject = false (which isn't working).

    Instead when I instantiate my object, it is created in the center of my screen, and it does not follow the mouse. Ontop of this, even when isPlacingObject = true, item1() will still instantiate another object, despite the fact that I check at the beginning of the method.

    Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    You should attach the debugger and put breakpoints on the lines that change isPlacingObject to see when it becomes false.

    Also you might want to look into Collections/Arrays/Lists and other iterative programming constructs so that you don't have to add another function like Item123 when you add each new item.
     
  3. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    I'll try that.

    I know how to use collections/arrays/lists ect. I'm using buttons with the new UI system, and they don't let you do anything with onClick() except call methods so I have to do it this way.
     
  4. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Okay so I'm noticing that whenever I hit the button that calls item1(), it turns isPlacingObject off. Once I let go of the button, it turns it back on again. This makes no sense, as I never told it to do any such thing.


    Anyone?

    Edit: by doing Debug.Log(currentItem.transform.position); it is printing the right coordinates to the console. However even if my console says (-2.5, 0.1, -4.6), the cube stays where it was instantiated. Also, by looking at it through the inspector, the XYZ doesn't change, even though the console says it is...

    Edit edit: Okay wtf, I have the currentItem PREFAB open in the inspector, and I can see the xyz changing the way it should, however the clone of it in my scene isn't moving at all. It's actually changing the prefab in my project folder instead of my object. How do I fix this?
     
    Last edited: Nov 18, 2015
  5. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    FIXED! It seems that I needed to instantiate my object like this:
    Code (CSharp):
    1. private void instantiateAtMousePosition(GameObject objectToInstantiate)
    2.     {
    3.         instObject = Instantiate(objectToInstantiate, Camera.main.ScreenToWorldPoint(Input.mousePosition), transform.rotation) as GameObject;
    4.         isPlacingObject = true;
    5.         currentItem = instObject;
    6.     }
    by storing the newly instantiated object under another variable, and then manipulating that object instead.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    functions can have parameters, like a string... having said that, the functions itemx() all do the exact same thing so I'm not sure why you have duplicated them.


    I suspect the problem you are having is that "Fire1" is the left mouse button, so in a single frame the button code is triggered (isPlacingObject = true) followed by the update code (isPlacingObject = false because "Fire1" is down). You'll need to check to see if the cursor is interacting with a UI element
     
  7. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    I haven't had time to change them, right now they're a copy paste. They're all going to have very diferent functions from one another once they're filled in.

    I'll look into checking this. I didn't think about that.