I’m creating a script that make some economy of performances:
i have a house in my scene that is heavy in polygons, so i created the same version of this house in lowpoly, and put it on the same place.
The goal is that when i am away from this house (50 units), the highPoly house disapear, and the lowPoly appear at the same time. When i’m closer, the lowPoly disapear and the highPoly appear.
i notice that all the 2 houses have some children so when some house disapear, the house’s children must disapear also, and this is the problem… here is my script:
var OperantA : Transform;// highPoly house
var OperantB : Transform;// Player
var LowPoly : Transform;// lowPoly house
var distance = 50;
function Update(){
if (Vector3.Distance(transform.position, OperantB.position) <distance){
OperantA.active = true;
LowPoly.active = false;
}
if (Vector3.Distance(transform.position, OperantB.position) >distance){
OperantA.active = false;
LowPoly.active = true;
}
}
with this code, the house GameObject is desactivated but not the childrens…
i tried to change the world “active” with “SetActiveRecursively” and happend the same…
Well GetComponentsInChildren only works when the childrens are active (so when the house is inactive, can’t turn active), and the script itself is not applied to the houses, because if it turn inactive, the script will not work for it come again active…
The sollution of BroadcastMessage seems to be much beter. Well i readed the script refference about it, but i don"t know how i have to write it in my script. Can you give me an idea?
(well i precise that this script can not be assigned to the objects that will appear / disapear, so the script must work for an object hiden that is on the same place then the houses but is only for measure the distance beetween the player and the house).
So i’m beggining in script and i don’t realy understand many words you use in the script so i don’t realy get the sense. Well can you just tell me how i can use it? Should i add this to my first script?
i realy tried your script in much ways, but nothing happend… can you be more clear please?
for now i finded a solution:
When the distance is away, the LowPoly is created with “instantiate” from a prefab and the Highpoly is destructed with “Destruct”. (All children also!! )
When the distance is close, the HighPoly is created wrom a prefab and the LowPoly is destructed, (the same way then before).
Well, for now, this script is enough for me, but i’m not very experimented in scripts so mabe someone could tell me a more simple solution, if it exist
Ah, yes, if someone is interrested to use this script that enable a best performance of 3d rendering from the computer with playing on the hi/lo Poly depending on the distance, i put it here
var Player : Transform;// dragdrop the player here in the hyerarchy
var HiPoly : Transform;// dragdrop the HiPoly complete Prefab
var LoPoly : Transform;//dragdrop the LoPoly complete Prefab
var distance = 100;//distance beetween the "mesure" object and the player... (the mesure object is an empty object that contain this script and define the place where the 2 objects will appear/disapear)
var score1 = 0;
var score2 = 0;
function Update(){
if ((Vector3.Distance(transform.position, Player.position) <distance) (score1 == 0)){
HiPolyYesLoPolyNo();
}
if ((Vector3.Distance(transform.position, Player.position) >distance) (score2 == 0)){
LoPolyYesHiPolyNo();
}
}
function HiPolyYesLoPolyNo(){
var LoPolyDestroy = GameObject.FindWithTag("TagFromLoPolyPrefab");//for destroy the LowPoly, we need his Tag (defined before making a prefab with)
score1 ++;
Instantiate(HiPoly, transform.position, transform. rotation);
Destroy(LoPolyDestroy);
score2 = 0;
}
function LoPolyYesHiPolyNo(){
var HiPolyDestroy = GameObject.FindWithTag("TagFromHiPolyPrefab");// the same here, we need a tag in this prefab, for he can be destroy after autocreation.
score2 ++;
Instantiate(LoPoly, transform.position, transform. rotation);
Destroy(HiPolyDestroy);
score1 = 0;
}
If I got that right, your hierarchy looks something like this
House > LowPolyHouse > Something else
House > HighPolyHouse > Something else
This code worked at my workspace when hierarchy was something like above. I did similar script few moments ago in similar purposes as you are. This is base idea from that. Everything is initially deactivated and after 3 seconds highpoly house is activated. In my workspace also those childrens gets activated.