Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Resolved Moving character up problem

Discussion in 'Scripting' started by alexxgolovanov, Nov 28, 2021.

  1. alexxgolovanov


    Mar 5, 2021
    Hello everyone! I have problem that connected with moving up gameobjects. I'm coding game Tower Builders where the crane moving from other side of screen to another and when player click on the screen, instantiated Cube is falling to the platform. Several the same cubes will create construction. How to code when the cube falls, the crane will lift up for small distance? Crane creates primitive cube and cubescript adds to the created cube as component, I don't have problems there.

    Here's my code:

    Main Code for touch input (under character I mean model of my crane and camera)
    Code (CSharp):
    1. using UnityEngine;
    2. //this all works allright as I wanted
    3. public class CharacterScript : MonoBehaviour
    4. {
    5.     [SerializeField] private GameObject crane;
    6.     private GameObject block;
    7.     void Update()
    8.     {
    9.         if (Input.GetMouseButtonDown(0))
    10.         {
    11.            if(gameObject.GetComponent<OpenGameScript>().isGameStarted == false)
    12.             {
    13.                 CreatingBlock();
    14.             }
    15.             else
    16.             {
    17.                 crane.GetComponent<Animation>().Stop();
    18.                 block.transform.parent = null;
    19.                 block.AddComponent<Rigidbody>();
    20.             }
    21.             gameObject.GetComponent<OpenGameScript>().isGameStarted = true;
    22.         }
    23.     }
    24.     public void CreatingBlock()
    25.     {
    26.         crane.GetComponent<Animation>().Play();
    27.         block = GameObject.CreatePrimitive(PrimitiveType.Cube);
    28.         block.transform.parent = crane.transform;
    29.         block.gameObject.AddComponent<BlockScript>();
    30.         block.transform.localScale = new Vector3(0.4f, 0.4f, 0.4f);
    31.         block.transform.position = new Vector3(crane.transform.position.x + 0.15f, crane.transform.position.y - 0.1f, crane.transform.position.z);
    32. = "Block";
    33.     }
    34. }
    This is the block script and I want when the cube touches another cube, my character will be moved up:

    Code (CSharp):
    1. using UnityEngine;
    3. public class BlockScript : MonoBehaviour
    4. {
    5.     private GameObject character;
    6.     private void OnCollisionEnter(Collision collision)
    7.     {
    8.         if ( == "Block")
    9.         {
    10.             character = GameObject.FindGameObjectWithTag("Character");
    11.             character.GetComponent<CharacterScript>().CreatingBlock();
    12.             character.transform.localPosition = new Vector3(character.transform.position.x, character.transform.position.y + 0.35f, character.transform.position.z); //here is the problem, I can't calculate Vector y that I need
    13.             Destroy(gameObject.GetComponent<BlockScript>());
    14.             Destroy(gameObject.GetComponent<Rigidbody>());
    15.         }
    16.     }
    17. }

    Hope for your soon reply!
  2. alexxgolovanov


    Mar 5, 2021
    Ok, I'm very frustrated because nobody replied this thread and helped me. I've solved this problem by myself. But for future visitors of this thread with similar/same problem I will give you a solution. My character (crane) gets a transform.position.y of last fallen cube by checking exisiting component of last cube (others don't have this component), then the character adds to this transform.position.y float var which will lift character up. That's a solution, later I will pin here code example.
  3. Kurt-Dekker


    Mar 16, 2013
    Excellent. Here is a more general-purpose solution that will work for ALL future bugs.

    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.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    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: