Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mealone · Jun 19, 2014 at 04:33 PM · instantiatearrayvector3list

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mealone · Jun 19, 2014 at 06:53 PM 0
Share

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?

avatar image AdamAndersson · Jun 19, 2014 at 08:25 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges