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 danivdwerf · Jul 29, 2016 at 06:57 PM · c#unity 5

My SetParent doesn't do what I want

Hi fellow developers :)

I wrote some code that instantiates a GameObject that shows the amount of damage you did. It worked, but it didn't work perfectly so I changed some stuff. Now every time I hit an enemy it spawns the text at the same enemy (like I hit enemy 2 but it spawns the text at enemy 1) :(

This is the function that I put on the canvas of the enemy prefab


    public void Initcbt(string damage)
    {
        GameObject temp = (GameObject)Instantiate(cbt);
        RectTransform tempRect = temp.GetComponent();
        temp.transform.SetParent(transform);
        tempRect.transform.localPosition = cbt.transform.localPosition;
        tempRect.transform.localScale = cbt.transform.localScale;
        tempRect.transform.localRotation = cbt.transform.localRotation;
        temp.GetComponent().text = damage;
        Destroy(temp.gameObject, 1f);
    }

I'm not sure if I'm using setParent right, I did it different but Unity was stalking me to death that I should use this one.

the script I wrote to for doing damage on the player is this:


if (Input.GetButtonDown("Hit1") && Stamina.stam.curStamina >= 20)
{
    Stamina.stam.curStamina -= takeStamina;
    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
    {
        distance = hit.distance;
        //Debug.DrawLine(transform.position, hit.transform.position, Color.red);
        if (distance < maxDistance)
        {
            damage = Random.Range(45.0f, 60.0f) + (PlayerStates.playerStates.level * 5);
            EnemyHealth.enemyHP.ApplyDamage(damage);
         }
    }
}  

it shoots a ray because i'm still waitng on my artists to finish the player (i'm sure you know the problem)

this is the function the player calls when it hits the enemy:

 public void ApplyDamage(float damage)
 {
     health -= damage;
     CombatText.combatText.Initcbt(damage.ToString());
 }


Help me please :)

Comment
Add comment · Show 3
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 allan36j · Jul 29, 2016 at 09:24 PM -1
Share

Did you try this:
temp.transform.parent = transform;

avatar image Firedan1176 allan36j · Jul 29, 2016 at 11:10 PM 0
Share

This is the deprecated way.

avatar image danivdwerf allan36j · Jul 30, 2016 at 12:03 PM 0
Share

@allan36j that how I did it, but Unity kept spam$$anonymous$$g my console that I shouls use SetParent

2 Replies

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

Answer by Firedan1176 · Jul 29, 2016 at 11:11 PM

Your script looks fine. The only issue I could possibly think of is if you are not calling Initcbt on the correct enemy. You may be calling it on Enemy1 when you are damaging Enemy2.

If this is not the case, please let me know. Also, please share the script where you call Initcbt.

Comment
Add comment · Show 4 · 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 danivdwerf · Jul 30, 2016 at 08:55 AM 0
Share

He @Firedan1176, thanks for commenting

the script I wrote to for doing damage on the player is this:


if (Input.GetButtonDown("Hit1") && Sta$$anonymous$$a.stam.curSta$$anonymous$$a >= 20)
{
    Sta$$anonymous$$a.stam.curSta$$anonymous$$a -= takeSta$$anonymous$$a;
    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
    {
        distance = hit.distance;
        //Debug.DrawLine(transform.position, hit.transform.position, Color.red);
        if (distance < maxDistance)
        {
            damage = Random.Range(45.0f, 60.0f) + (PlayerStates.playerStates.level * 5);
            EnemyHealth.enemyHP.ApplyDamage(damage);
         }
    }
}  

it shoots a ray because i'm still waitng on my artists to finish the player (i'm sure you know the problem)

this is the function the player calls when it hits the enemy:


    public void ApplyDamage(float damage)
    {
        health -= damage;
        CombatText.combatText.Initcbt(damage.ToString());
    }

avatar image Firedan1176 danivdwerf · Jul 30, 2016 at 07:24 PM 0
Share

I'm thinking EnemyHealth.enemyHP is just one object. If I were you, I'd try to get the enemy script attached based off of the RaycastHit hit that you defined:

 hit.transform.GetComponent<EnemyHealth>().enemyHP.ApplyDamage()...

Also, you can simplify transform.TransformDirection(Vector3.forward) to transform.forward, as it is local to the transform.

Let me know if the above fixes the problem.

avatar image danivdwerf Firedan1176 · Jul 31, 2016 at 07:06 PM 0
Share

It worked man. I can't see the text, but that's another problem. The text get Instantiated on the canvas of the enemy I'm hitting.

THAAAAAAN$$anonymous$$S

Show more comments
avatar image
0

Answer by Nitin22 · Jul 30, 2016 at 08:57 PM

Hello @danivdwerf , I think you should change your approach to show UI on player . Because Canvas always recalculate there position according to there settings . So I think you write down a script that update the UI panel where data is shown according to player screen position without making it parent.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Only Allow One Click on GameObject? 2 Answers

Changing one script through another? 2 Answers

How can I do this with gizmos? 0 Answers


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