Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 nwmarkovic · Feb 16, 2018 at 10:44 PM · gameobjectinstantiatecloneyaxisxaxis

Instantiate GameObject cloned into wrong position.

I have floating number script that I want to be instantiated every time the sword hits a enemy, the number will display the damage. The issue I have is that every time the prefab object is put in the wrong x and y position. This is a 2D game. An example is that if my hitPoint, where the object will be instantiated, is (78,-36.5,.8) the floating text will be at (0,0,.8). Here is a picture of how the hitPoint is situated, the sword has the collision box and the player holds the sword. alt text This is the first script that takes care of the instantiation of the object,

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HurtEnemy : MonoBehaviour {
 
     public int damageToGive;
     public GameObject damageBurst; //Blood particle effect
     public Transform hitPoint;  //Brings in point from game
     public GameObject damageNumber; //Used to give the floating number a damage number
     
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Enemy")
         {
             Debug.Log(hitPoint.position); //For troubleshooting reasons
 
             other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToGive); //This is for the blood particle effect
             Instantiate(damageBurst, hitPoint.position, hitPoint.rotation); //This is for the blood particle effect
 
             GameObject clone = Instantiate(damageNumber, hitPoint.position, Quaternion.identity) as GameObject; //This is where the issue may be occuring
             clone.GetComponent<FloatingNumbers>().damageNumber = damageToGive; //Changes the damage number in another script.
             Debug.Log(hitPoint.position);//For troubleshooting reasons
 
         }
     }
    
 }

Here is the script that this one calls for the FloatingNumbers,

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class FloatingNumbers : MonoBehaviour {
 
     public float moveSpeed; //Speed of the floating numbers
     public int damageNumber; //Damage that it displays
     public Text displayNumber; //The display
     
 
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
         displayNumber.text = "" + damageNumber; //Turning float into string
         transform.position = new Vector3(transform.position.x, transform.position.y + (moveSpeed * Time.deltaTime), transform.position.z); //Making it float
         Debug.Log(transform.position); //For troubleshooting
     }
 }
 

Here are the Debug.logs that are produced for troubleshooting\ alt text

screenshot-9.png (5.0 kB)
screenshot-10.png (29.2 kB)
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 nwmarkovic · Feb 17, 2018 at 12:20 AM

OK I figured it out, to fix this situations there are two ways, the first is to add

 clone.transform.position = hitPoint.position;

after the Debug.Log in the first script and the second way is to rewrite the code for instantiate as,

 GameObject clone = Instantiate(damageNumber) as GameObject;
 clone.transform.position = hitPoint.position;
 clone.transform.rotation = Quaternion.identity;

These are the two ways that solved the problem for me.

Comment
Add comment · 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

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

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to Clone 1 of 5 GameObjects in a array 1 Answer

How can I destroy an specific clone already instantiated, when there's more than one? 1 Answer

Clone Selector 1 Answer

Instantiated Object spawning at wrong position 3 Answers

Assign an instantiated GameObject? 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