- Home /
Intantiated objects deleting themselves automatically.
I have a script which instantiates object(land) like to create a infinite land in one direction, here is the script.
var land : GameObject;
function Start () {
}
function Update () {
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "Player")
{
var landins = Instantiate(land, Vector3(0,0, transform.position.z + 10), Quaternion.identity);
landins.name = "Land";
}
}
function OnTriggerExit(other : Collider)
{
if(other.tag == "Player")
{
Destroy(gameObject);
}
}
It works perfectly awesome, but after some time(Infact after exact time or to say at a exact location) this script stops working, i.e. it stops instantiating objects and automatically deletes obejects it has instantiated except the last one.
I cant figure the problem itself.
Note - My Scene is completely empty it only has a land block and player.
EDIT - i somehow figured out what's happening at the last block(at z co-ordiante 450) instead of instantiating next block at 460 it is instantiating the block at 450 itself, which is causing this problem. any ideas, why?
Conclusion I Reached - First when i start from z-cordinate 0 than i am able to reach 450 before if stops spawning the next at more distance, second, if i start at 450, i am able to reach 2300 Z-axis but it stops at 2300 z-axis as at it stopped at 450.
Are you incrementing the speed of the player over time? $$anonymous$$aybe you player goes that fast that it basically rushes over your block in less than one frame, Preventing both OnTriggerEnter and OnTriggerExit from being called.
To me the code seems legit enough to continue building blocks each 10 units on the Z axis. However a lot of times strange things happen in specific situations and sometimes for other people is not easy enough to spot a particular bug. $$anonymous$$aybe you can provide the demo project as it is so it would be easier for people to see exactly what is happening and try to help you.
ah actually i think the player's speed or physics was messing it. i just imported first person controller and set it's speed to 1000, and yet it was working perfectly fine, while i tried putting the speed to 500 in my controller and the script(intantiater) stopped at the very first moment, so what i did was removed the capsule collider from my controller and set the initial speed to zero while incrementing the speed, and it now works fine, atleast i tried a few times, and it reached upto 15,000 without a problem. so i think either the initial speed was messing or the capsule collider...
Try to scale your cubes and terrain overall to smaller units, it will be much more easier for you to work while program$$anonymous$$g.
Answer by Sarthak123 · Nov 03, 2014 at 04:05 PM
hmm, i think its due to the initial speed, if i start the player with the speed 0, and then increment it like a car, then everything's work fine, however if i preset the speed, to something greater than 5 or 10, then it causes a bug at 450 co-ordiante and nearby per increase/decrease in speed, so i think initial speed was the problem, i will check again after adding some more entities... to check if it's still the problem or not.
thanks for suggesting the hints by the way
Your answer
Follow this Question
Related Questions
Is it possible to put a world space UI onto an instantiated object? 1 Answer
Using Resources.load with a variable 1 Answer
Change one intance, change all the prefab - Not found the correct answer in other threads 1 Answer
Making multiple instances of a prefab,Instantiating multiple instances of a prefab 1 Answer