Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

if object is hit create object help?

Discussion in 'Scripting' started by bonersoup, May 31, 2015.

  1. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    Ok so what i wanted to do was make it so that if you hit a certain object it creates another object...
    what ive done so far
    Created a rock (The object i want to hit)
    Created the ore (the object that should be created)

    assets being used: UFPS, InventoryPro...
    so what do i do from here?
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    You will have to instantiate the ore as a new gameobject.
     
  3. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    Im sorry... could you explain a little farther im a noob...
     
  4. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Hm, well, the article does describe it. :)
    How about a tutorial video instead? If the tutorial doesn't help, could you please be more specific at which point you're struggling?
     
  5. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    You should name your 'rock' object and apply this code for example to the main camera, and, obviously, attach a Collider to your rock.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class YourCodeName : MonoBehaviour
    7. {
    8.   public int TouchCount = Input.touchCount;
    9.   public RuntimePlatform UsedPlatform = Application.platform;
    10.   public GameObject ore;
    11.  
    12.   // Update is called once per frame
    13.   void Update()
    14.   {
    15.   if (UsedPlatform == RuntimePlatform.Android || UsedPlatform == RuntimePlatform.IPhonePlayer)
    16.   {
    17.   if (TouchCount == 1)
    18.   {
    19.   if (Input.GetTouch(0).phase == TouchPhase.Began)
    20.   {
    21.   checkTouch(Input.GetTouch(0).position);
    22.   }
    23.   }
    24.   }
    25.   else if (UsedPlatform == RuntimePlatform.WindowsEditor)
    26.   {
    27.   if (Input.GetMouseButtonDown(0))
    28.   {
    29.   checkTouch(Input.mousePosition);
    30.   }
    31.   }
    32.   }
    33.   void checkTouch(Vector3 pos)
    34.   {
    35.   var wp = Camera.main.ScreenToWorldPoint(pos);
    36.   var touchPos = new Vector2(wp.x, wp.y);
    37.   var hit = Physics2D.OverlapPoint(touchPos);
    38.   if (hit)
    39.   {
    40.   if (hit.name.Contains("TheNameYouGaveToTheRock"))
    41.   {
    42.   GameObject Clone = Instantiate(ore, new Vector3(theXpositionOfYourObjectYouWantToAppear, theYpositionOfYourObjectYouWantToAppear, theZpositionOfYourObjectYouWantToAppear), Quaternion.identity) as GameObject;
    43.   }
    44.   }
    45.   }
    46. }
    47.  
    48.  
     
  6. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    @Simonxzx ok so were do i put the name of the ore object?
    do i attatch this script to the terrain?
     
  7. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    No. You have to put a name at your Rock object
     
  8. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    Ok so the "Brassore" item witch is a inventory item witch i want to be created by hitting "BrassRock"

    So i attach this to the fps camera?
     
  9. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    Ok so lets say Game object 1 is a actual rock...
    and game object 2 is a smaller rock...
    if i hit object 1 it will create object 2...
    ?????
     
  10. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Replace "TheNameYouGaveToTheRock" with "BrassRock".
    Attach the script to the camera. Select the camera, and drag your Brassore prefab to the "ore" variable in the inspector.
    Tapping a "BrassRock" will instantiate a new "Brassore" gameobject.
     
  11. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
  12. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    for some reason its not creating the object xD
     
  13. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Well. Debug. ;D
     
    bonersoup likes this.
  14. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    LOL maybe i should just talk to a friend who knows more than i xD
     
  15. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    You know, it's hard to decide what's wrong with your code without actually seeing it. ;)
     
  16. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    Maybe you wanna talk on a teamspeak???? or Skype???
     
  17. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    get_touchCount can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

    ArgumentException: get_touchCount can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    Test..ctor ()
     
  18. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Put "public int TouchCount;" instead of "public int TouchCount = Input.touchCount;"
     
  19. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    Didnt work..
     
  20. bonersoup

    bonersoup

    Joined:
    Nov 11, 2013
    Posts:
    18
    i think it might be incompatable with ufps camera?