- Home /
Objects seemingly not being instantiated after touch.
I've been digging around and trying to find the answer to this question but found nothing. I'm trying to create a simple clicker game but got stuck at the very start... which is not very good. What I'm trying to do is after a touch give the player money (currently just adding +1$) and spawning in a text object saying "+1$". Seems like the object does not appear. I also made an "animation" which I'm not sure if it's well made. Both classes:
 public class MoneyTap : MonoBehaviour {
 public GameObject money;
 private Text moneyText;
 public long moneyAmount = 0;
 public GameObject tapText;
 private Vector2 tempPos;
 private Vector3 fullPos;
 void Start () {
     moneyText = money.GetComponent<Text> ();
 }
 
 // Update is called once per frame
 void Update ()
 {
     moneyText.text = moneyAmount + "$";
     if (Input.touchCount > 0) {
         if (Input.GetTouch (0).phase == TouchPhase.Began) {
             tempPos = Input.GetTouch(0).position;
             fullPos = new Vector3 (tempPos.x, tempPos.y, 0);
             
             RaycastHit2D hitInfo = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.GetTouch(0).position), Vector2.zero);
             // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
             if (hitInfo) {
                 moneyAmount++;;
                 Instantiate(tapText, fullPos, Quaternion.identity);
             }
         }
     }
 }
 }
And:
 public class TapTextMovement : MonoBehaviour {
 private int frameCount = 0;
 private float temp;
 public int modifier = 0;
 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void Update () {
     if (frameCount < 40) {
         temp = this.transform.position.y + 1F * modifier;
         this.transform.position = new Vector3(this.transform.position.x, temp, this.transform.position.z);
     } else if (frameCount < 50) {
         temp = this.transform.position.y + 0.5F * modifier;
         this.transform.position = new Vector3(this.transform.position.x, temp, this.transform.position.z);
     } else if (frameCount < 55) {
         temp = this.transform.position.y + 0.3F * modifier;
         this.transform.position = new Vector3(this.transform.position.x, temp, this.transform.position.z);
     } else if (frameCount < 60) {
         temp = this.transform.position.y + 0.15F * modifier;
         this.transform.position = new Vector3(this.transform.position.x, temp, this.transform.position.z);
     } else {
         Object.Destroy (this);
     }
     frameCount++;
 }
 }
I am a bit new to Unity so every bit of help would be appreciated! Thanks!
Answer by GizmoKaracho · Sep 01, 2017 at 12:25 PM
  RaycastHit2D hitInfo = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.GetTouch(0).position), Vector2.zero);
              // RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
              if (hitInfo) {
                  moneyAmount++;;
                  Instantiate(tapText, fullPos, Quaternion.identity);
              }
          }
I think you want to create an Object in World Space, so you need World Coordinates
 Instantiate(tapText, hitInfo.transform.position , Quaternion.identity);
So you create the GameObject where your Raycast hits World Space. fullPos is your touch position (in pixel), so you won't see it
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                