Search Unity

Resolved How do I change the parent of an object

Discussion in 'Scripting' started by jlorenzi, May 30, 2022.

  1. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    293
    Edit: I'm actually blind to be honest, I set the wrong layer in the layermask I was using and I somehow saw the 'b' in the console as an 'a' and thought both the log statements were running. I'm stupid.

    I'm trying to make a slime that absorbs things, but when I set the position, rotation, and parent of the block the slime it's absorbing, not a single thing changes.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BasicSlime : MonoBehaviour
    4. {
    5.     public float speed = 300;
    6.     public float takeDelay;
    7.     public float rotSpeed;
    8.     public LayerMask playerBlockLayer;
    9.  
    10.     CharacterController controller;
    11.     float nextFireTime;
    12.  
    13.     private void Start()
    14.     {
    15.         controller = GetComponent<CharacterController>();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         controller.SimpleMove(transform.forward * Time.deltaTime * speed);
    21.  
    22.         transform.eulerAngles += new Vector3(0, 0, rotSpeed * Time.deltaTime);
    23.     }
    24.  
    25.     private void OnControllerColliderHit(ControllerColliderHit hit)
    26.     {
    27.         Debug.Log("B");
    28.         if ( Time.time > nextFireTime && hit.gameObject.layer == playerBlockLayer )
    29.         {
    30.             Debug.Log("A");
    31.             nextFireTime = Time.time + takeDelay;
    32.  
    33.             hit.transform.parent = transform;
    34.             hit.transform.position = transform.position + new Vector3(Random.Range(-5, 5), Random.Range(-5, 5), Random.Range(-5, 5));
    35.             hit.transform.eulerAngles += new Vector3(Random.Range(-360, 360), Random.Range(-360, 360), Random.Range(-360, 360));
    36.         }
    37.     }
    38. }
    39.  
    The two log statements run, but the block it's absorbing doesn't change at all.
     
    Last edited: May 30, 2022