Hi
So what I have is a cube with this script attached to it.A cild of the cube is another object that i have in unenabled mode.But when the cube comes in view it throws this error.
Enabling or adding a Renderer during rendering; this is not allowed. Renderer ‘LOD0_Plant_LOD_0’ will not be added.
UnityEngine.GameObject:SetActive(Boolean)
isvis:OnBecameVisible() (at Assets/0myscripts/isvis.cs:9)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class isvis : MonoBehaviour {
void OnBecameVisible() {
this.transform.GetChild(0).gameObject.SetActive (true);
}
}
If your only objective is to save resources when the object is not in view, Unity already does ‘Frustum Culling’.
It sounds like you cant do this as it is too late into the rendering of the frame for you to affect the visibility of objects. Unity event order HERE.
A workaround would be to start a coroutine which simply waits a frame before enabling the object.
Well I have thousands of objects that need to run a script when first activated to limit there impact on the game but to have them all load at once crashes my system so this way i can space the loading out.This script will only run once then be deactivated with the main object only active from then.
Tried adding a count before it renders but same problem:(
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class isvis : MonoBehaviour {
int a=0;
void Update(){
a = a + 1;
}
void OnBecameVisible() {Debug.Log (a);
if (a > 111)
{this.transform.GetChild (0).gameObject.SetActive (true);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class isvis : MonoBehaviour {
void OnBecameVisible() {
for(int a = 0; a < 111; a=a+1) Debug.Log (a);
{this.transform.GetChild (0).gameObject.SetActive (true);
}
}
}
Modified an example i found and it works no idea what the code means bit of a noob but long as it works i guess.Strange thing us though the sript is deactivated tut still renders the object when i turn it off and look away then back it turns the object back on even though the script remains switched off.How does this xscript run when its inactive?
think this is what u meant Tale?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class isvis : MonoBehaviour {
void Start(){
}
IEnumerator OnBecameVisible () {
yield return new WaitForEndOfFrame();
this.transform.GetChild (0).gameObject.SetActive (true);
this.enabled = false;
}
}