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. Dismiss Notice

[Solved] Renderer.enable or MeshRenderer.enable not working on moving objects

Discussion in 'General Graphics' started by darkesco, Jan 31, 2022.

  1. darkesco

    darkesco

    Joined:
    Mar 19, 2015
    Posts:
    61
    I have an occluder script on multiple objects in the scene by detecting a boxcollider attached to my moving player. All objects work fine, except some moving objects. Here is the basic occulder script (placed on all objects that need to be turned on when the player gets near them:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OcclusionControl : MonoBehaviour
    6. {
    7.     private Renderer Rend;
    8.  
    9.     // Start is called before the first frame update
    10.     void Awake()
    11.     {
    12.         Rend = this.gameObject.GetComponent<Renderer>();
    13.         Rend.enabled = false;
    14.     }
    15.  
    16.     private void OnCollisionEnter(Collision collision)
    17.     {
    18.         if (collision.rigidbody.gameObject.CompareTag("tagOccluder"))
    19.         {
    20.             Rend.enabled = true;
    21.             Debug.Log("RENDERE SET TO TRUE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    22.         }
    23.     }
    24.  
    25.     private void OnCollisionExit(Collision collision)
    26.     {
    27.         if (collision.rigidbody.gameObject.CompareTag("tagOccluder"))
    28.         {
    29.             Rend.enabled = false;
    30.             Debug.Log("RENDERE SET TO false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    31.         }
    32.     }
    33.  
    34.     private void OnTriggerEnter(Collider other)
    35.     {
    36.         Debug.Log("Trigger Enter::::::::::: Name:" + other.name + " Tag:" + other.tag);
    37.         if (other.CompareTag("tagOccluder"))
    38.         {
    39.             Rend.enabled = true;
    40.             Debug.Log("RENDERE SET TO TRUE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    41.             Debug.Break();
    42.         }
    43.     }
    44.  
    45.     private void OnTriggerExit(Collider other)
    46.     {
    47.         if (other.CompareTag("tagOccluder"))
    48.         {
    49.             Rend.enabled = false;
    50.             Debug.Log("RENDERE SET TO false!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    51.         }
    52.     }
    53.  
    54.  
    55.  
    56.  
    57. }
    This code works on static and non-static labeled objects, however, whgen I add this script that moves the non-static labeled objects. The render.enable code executes, but the inspector checkbox for meshrender on the object stays off and the object is still not visible.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. /****************************************************************************
    6. /Purpose: Move any non-static object back and forth or up and down
    7. /Example: Moving obsticles that the player will need to dodge
    8. / ***************************************************************************/
    9.  
    10. public class MoveBackAndForth : MonoBehaviour
    11. {
    12.     //Editor
    13.     [SerializeField] private bool isZAxisMovement;
    14.     [SerializeField] private float MaxAxisValue;
    15.     [SerializeField] private float MinAxisValue;
    16.     [SerializeField] private float CurrentAxisValue;
    17.     [SerializeField] private float SpeedOfMovement;
    18.  
    19.     private Vector3 CurrentPosition;
    20.  
    21.     private bool SwapDirection;
    22.  
    23.     private void Awake()
    24.     {
    25.         if(CurrentAxisValue < MinAxisValue)
    26.         {
    27.             CurrentAxisValue = MinAxisValue;
    28.         }
    29.  
    30.         if(CurrentAxisValue > MaxAxisValue)
    31.         {
    32.             CurrentAxisValue = MaxAxisValue;
    33.         }
    34.  
    35.         CurrentPosition = transform.position;
    36.  
    37.         if (isZAxisMovement == true)
    38.         {
    39.             CurrentPosition.z = CurrentAxisValue;
    40.         }
    41.         else
    42.         {
    43.             CurrentPosition.x = CurrentAxisValue;
    44.         }
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.         MoveObject();
    51.      
    52.     }
    53.  
    54.     private void MoveObject()
    55.     {
    56.         if (isZAxisMovement == true)
    57.         {
    58.  
    59.             if (SwapDirection == false)
    60.             {
    61.                 CurrentAxisValue += (SpeedOfMovement * Time.deltaTime);
    62.                 if (CurrentAxisValue > MaxAxisValue)
    63.                 {
    64.                     CurrentAxisValue = MaxAxisValue;
    65.                     SwapDirection = !SwapDirection;
    66.                 }
    67.             }
    68.             else
    69.             {
    70.                 CurrentAxisValue -= (SpeedOfMovement * Time.deltaTime);
    71.                 if(CurrentAxisValue < MinAxisValue)
    72.                 {
    73.                     CurrentAxisValue = MinAxisValue;
    74.                     SwapDirection = !SwapDirection;
    75.                 }
    76.             }
    77.  
    78.             CurrentPosition.z = CurrentAxisValue;
    79.             transform.position = CurrentPosition;
    80.         }
    81.         else
    82.         {
    83.             if (SwapDirection == false)
    84.             {
    85.                 CurrentAxisValue += (SpeedOfMovement * Time.deltaTime);
    86.                 if (CurrentAxisValue > MaxAxisValue)
    87.                 {
    88.                     CurrentAxisValue = MaxAxisValue;
    89.                     SwapDirection = !SwapDirection;
    90.                 }
    91.             }
    92.             else
    93.             {
    94.                 CurrentAxisValue -= (SpeedOfMovement * Time.deltaTime);
    95.                 if (CurrentAxisValue < MinAxisValue)
    96.                 {
    97.                     CurrentAxisValue = MinAxisValue;
    98.                     SwapDirection = !SwapDirection;
    99.                 }
    100.             }
    101.  
    102.             CurrentPosition.x = CurrentAxisValue;
    103.             transform.position = CurrentPosition;
    104.         }
    105.        
    106.     }
    107. }
    I have no idea why moving the objects would cause the renderer.enabled to fire, but not do anything.
     
  2. darkesco

    darkesco

    Joined:
    Mar 19, 2015
    Posts:
    61
    Fixed it. Even though the parent object containing the move script doesn't have a collider on it (child objects have the collider, and the occlusion script), I added a rigidbody, set kinematic, and changed Update to FixedUpdate on the move script and now it works. I probably don't understand enough to know why the parent would need the rigidbody, but it's working now.
     
  3. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,068
    darkesco likes this.
  4. darkesco

    darkesco

    Joined:
    Mar 19, 2015
    Posts:
    61
    So, this was not an issue with moving objects with a parent. They move fine. It was an error in the move script that moved the objects along the wrong axis. Renderer.Enable worked fine and there is no need to have any rigidbody or collider on the parent. The movable objects were visible and moving, the script just placed the objects far away on the scene before moving them. It was a simple coding error and I don't want to confuse anyone coming here in the future. When you have an object disappear, select the "invisible" object and press "f" to locate it in the scene. Make sure it is actually invisible/missing. Silly error, but I wanted to clarify.
     
    BrandyStarbrite likes this.