- Home /
The question is answered, right answer was accepted
Stop instantiate on collision from other script
Hey guys,
This question comes in completion of my previous one "How to limit instantion of prefabs/gameobjects".
So I have a script called "CreateWall" that I use to instantiate objects in certain directions on mouse click&drag.
The instantiation code looks something like this:
function SpawnFence()
{
if (sss == 1 && !someCol.someBool)
{
position_var = clone2.position + Vector3(0,20,0);
clone3 = Instantiate(wallObject, position_var, Quaternion.identity);
clone3.parent = parentofwall;
clone3.transform.position.z = 0;
last_clone = clone3;
if(!someCol.someBool){
sss = 2;
}
}
and it goes on and one until the last clone.
The collision script is currently set on the object that is being instantiated and it has the following code:
function Awake()
{
wallCreate = GameObject.Find("Background - GameManager").GetComponent.<CreateWall>();
someBool = false;
}
function OnTriggerEnter(hit:Collider)
{
if(hit.collider.tag == "SomWall")
{
wallCreate.sss = 0;
Debug.Log ("collided");
someBool = true;
}
}
Here's my problem. The sss var is being set to 0 in my CreateWall, my someBool is becoming true right when colliding but i think objects are instantiating too fast for the script to have time to run through this. Should I slow it down using WaitForSeconds or should I rewrite my whole script?
SEE LAST COMMENT FOR MORE INFO ON THE FIX!
Answer by DannyB · May 15, 2013 at 08:38 PM
A few things are noticeable:
First, it would always be easier for you (and for us to understand) if you give the variables more meaningful names.
Now, as for the essence of this question - I would assume your problem is that the sss variable is being controlled by both an outside source (your collision detection) and the condition itself, which I can assume is in a loop.
So, as a first test, separate the two. Let the collider control a different variable that is untouched by anyone other then the collider itself ( like wallCreate.isCreationEnabled = false
).
If I am reading your code correctly, I have a feeling this approach will point you in the right direction.
Thank you for the tip, I actually already did both those things with my code. And the collision is triggered (I see the debug message) and also a boolean becomes true.
I'm not sure where my problem stands. I've tried to turn the boolean to static (so I can access it from the other script) but I'm not sure whether it's triggered or not, although the debug message is still sent).
I see. There is another alternative to static or GameObject.Find()
that may help you - the "pseudo-singleton" of Unity.
In your WallCreate object, make a variable public static WallCreate instance;
, then in its Awake()
do instance = this;
, and finally, you can now get a reference to it statically, by using WallCreate.instance
ins$$anonymous$$d of your GameObject.Find
.
Fixed. I'm marking your answer as correct because it's close enough!
If someone ever needs more info on this - I solved it by creating clones only if a variable in the other script was not met (eg: the collision did not occur).
Follow this Question
Related Questions
How To: Spawn object on raycast collision. 2 Answers
Collision with tag 5 Answers
Issues with instantiated prefabs 1 Answer
Set parent of instantiated object. 0 Answers