- Home /
instantiate problem help?
hello all, i have a list which contains vector3's from where two or more gameobjects collide. the player swipes the swipe gameobject across the screen, and when it collides with the other gameobjects in the scene (balloons), they burst and are destroyed. when the player lifts the finger the swipe gameobject is destroyed and when he touches the screen the swipe gameobject is created and constrained to the finger or mouse position for movement. i am trying to set up some code so that when the swipe g.o collides with the balloons, it stores the vector3 position of the balloons, then checks the number of collisions which occurred, and then spawns a combo prefab at the last hit balloon position. eg. swipe gameobject hits multiple balloons, the vector3 positions are added to the list and then the code checks to see how many hits occurred before the user lifts the finger or mousebutton and ends the swipe, then spawns the combo prefab at the last collision vector3 point stored in the list.
this is the code that i have tried: var myHitList : List. = new List.(); var combo : GameObject; var hitpoint : Vector3 = new Vector3();
function OnCollisionEnter2D(coll: Collision2D) {
if (coll.gameObject.tag == "swipe"){
myHitList.Add(coll.gameObject.transform.position);
}
}
function Update() {
if (Input.GetMouseButtonUp(0)) {
myHitList.Clear();
}
if(myHitList.Count == 2 )
{
Instantiate(combo, myHitList[1], Quaternion.identity);
}
}
this works and spawns the combo at the position of the second hit balloon, but there are a few errors that i cannot seem to figure out. the combo prefab spawns once every frame for the duration of the swipe, and i would like it to spawn only one time, additionally i am not sure as to how to check for multiple hits before spawning the combo at the vector3 point. eg. if 5 balloons are hit before swipe ends, how do i check the number of hits first and then spawn the combo? i would not want a combo spawning at each collision point more than 2, but would only want it spawned at the last hit position. i am fairly new to unity, if someone could help me out in fixing this code and with these problems i would really appreciate it! thanks in advance!
Answer by AdamAndersson · Jun 19, 2014 at 05:35 PM
Hello!
If I understand you correctly, you want something like this in your update function:
function Update() {
if (Input.GetMouseButtonUp(0)) {
if(myHitList.Count >= 2)
{
Instantiate(combo, myHitList[myHitList.Count()-1], Quaternion.identity);
}
myHitList.Clear();
}
}
I'm guessing that the swipe ends at Input.GetMouseButtonUp. You then check the list and see if the list is larger or equal to 2. If it is, you instantiate the combo prefab at the position last saved in myHitList.
Good luck, and hope this helps :)
hey, thank you so much for the response. i tried the code in my update function but got these errors in the console : "It is not possible to invoke an expression of type 'int'." "The best overload for the method 'System.Collections.Generic.List..get_Item(int)' is not compatible with the argument list '(error)'."
i think this is giving the error - ''myHitList[myHitList.Count()-1]'' do you have an idea as to what could be causing this?
I never tried to compile the code, sorry about that. I think you need to replace "myHitList[myHitList.Count()-1]" with "myHitList[myHitList.Count-1]", but I do not have the possibility to compile it atm so I'm not 100% sure.
Your answer
Follow this Question
Related Questions
How would i use a 2D array for level generation 0 Answers
Random select from array and spawn 1 Answer
How to animate a list of GameObject from their localposition 1 Answer
Get size of a vector3 array c# 1 Answer
How can I instantiate parented ui objects above previously instantiated children? 1 Answer