Search Unity

problem with cubes and grid

Discussion in 'Scripting' started by julien83franceschi, Sep 24, 2021.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    Hello to all,

    I want to make the following tutorial
    (you have to place cubes in a grid)



    I can make the grid, but I can not create cubes by clicking on the white image (sort of white plane below the grid).

    Here is my code of the tutorial, there is probably an error (but the debugger show no errors )

    1. PLaceObjectOnGrid
    Code (CSharp):
    1. public class PlaceObjectOnGrid : MonoBehaviour
    2. {
    3.  
    4.     public Transform gridCellPrefab;
    5.     public Transform cube;
    6.     public Transform onMousePrefabe;
    7.     public Vector3 smoothMousePosition;
    8.     [SerializeField] private int height;
    9.     [SerializeField] int width;
    10.  
    11.     private Vector3 mousePosition;
    12.     private Node[,] nodes;
    13.     private Plane plane;
    14.  
    15.     void Start()
    16.     {
    17.         CreateGrid();
    18.         plane = new Plane(Vector3.up, transform.position);
    19.     }
    20.  
    21.  
    22.     void Update()
    23.     {
    24.         GetMousePositionOnGrid();
    25.     }
    26.  
    27.     void GetMousePositionOnGrid()
    28.     {
    29.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    30.         if (plane.Raycast(ray, out var enter))
    31.         {
    32.             mousePosition = ray.GetPoint(enter);
    33.             print(mousePosition);
    34.             smoothMousePosition = mousePosition;
    35.             mousePosition.y = 0;
    36.             mousePosition = Vector3Int.RoundToInt(mousePosition);
    37.             foreach (var node in nodes)
    38.             {
    39.                 if (node.cellPosition == mousePosition && node.isPlaceable)
    40.                 {
    41.                     if (Input.GetMouseButtonUp(0) && onMousePrefabe != null)
    42.                     {
    43.                         node.isPlaceable = false;
    44.                         onMousePrefabe.GetComponent<ObjFollowMouse>().isOnGrid = true;
    45.                         onMousePrefabe.position = node.cellPosition + new Vector3(0, 0.5f, 0);
    46.                         onMousePrefabe = null;
    47.                     }
    48.  
    49.                 }
    50.             }
    51.         }
    52.  
    53.     }
    54.  
    55.     public void OnMouseClickOnUI()
    56.     {
    57. //       if (onMousePrefabe == null)
    58.         {
    59.             onMousePrefabe = Instantiate(cube, mousePosition, Quaternion.identity);
    60.         }
    61.     }
    62.  
    63.     private void CreateGrid()
    64.     {
    65.         nodes = new Node[width, height];
    66.         var name = 0;
    67.         for (int i = 0; i < width; i++)
    68.         {
    69.             for (int j = 0; j < height; j++)
    70.             {
    71.                 Vector3 worldPosition = new Vector3(i, 0, j);
    72.                 Transform obj = Instantiate(gridCellPrefab, worldPosition, Quaternion.identity);
    73.                 obj.name = "Cell " + name;
    74.                 nodes[i, j] = new Node(true, worldPosition, obj);
    75.                 name++;
    76.             }
    77.         }
    78.     }
    79.  
    80.  
    81.  
    82.  
    83.     public class Node
    84.     {
    85.         public bool isPlaceable;
    86.         public Vector3 cellPosition;
    87.         public Transform obj;
    88.         public Node(bool isPlaceable, Vector3 cellPosition, Transform obj)
    89.         {
    90.             this.isPlaceable = isPlaceable;
    91.             this.cellPosition = cellPosition;
    92.             this.obj = obj;
    93.         }
    94.     }
    95. }
    96.  
    - And 2 ObjFollowMouse
    Code (CSharp):
    1. public class ObjFollowMouse : MonoBehaviour
    2. {
    3.    private PlaceObjectOnGrid placeObjectOnGrid;
    4.     public bool isOnGrid;
    5.  
    6.     void Start()
    7.     {
    8.         placeObjectOnGrid = FindObjectOfType<PlaceObjectOnGrid>();
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if (! isOnGrid)
    16.         {
    17.             transform.position = placeObjectOnGrid.smoothMousePosition + new Vector3(0, 0.5f, 0);
    18.         }
    19.     }
    20. }
    Indeed, I can only place one cube in the grid, then I click and nothing happens.

    Your help is welcome,

    A+
     
    Last edited: Sep 24, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    Hello to all,

    I would like to create the cubes dynamically only when you click on the white image (see tutorial above) at 20min hairy pile.
    I have tried the following code, but it doesn't work;

    Code (CSharp):
    1.  
    2.  
    3. public Transform onMousePrefabe;
    4. public void OnMouseClickOnUI()
    5.     {
    6.             onMousePrefabe = Instantiate(cube, mousePosition, Quaternion.identity);
    7.     }
    8.  
    Thanks for your help,
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    What is calling this method? I've never heard of it and neither has Google.

    Why not just use an array of buttons? Or else put colliders on each cell and use the OnMouseDown() method.

    Make sure you read the documentation for how OnMouseDown() works.
     
  5. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    I use an Event Trigger :
    -I click on the add component object and select Event Trigger
    -I choose PointerClick
    -I push the + button at the bottom right
    -I put my Grid empty (it is an empty) in the object box
    -I choose PLaceObjectOnGrid and OnMouseClickOnUI in the function list

    I just want to create a cube when we click on the white image each time we click on it (see at 20
    min in the tuto)
    Thanks,

    A+
     
    Last edited: Sep 25, 2021