Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

[solved] UI Button - behavior of instantiate with prefab

Discussion in 'UGUI & TextMesh Pro' started by Achim, Nov 26, 2018.

  1. Achim

    Achim

    Joined:
    Dec 19, 2007
    Posts:
    199
    Hallo,
    I have a problem with a UI button, when I instantiate a prefab with onClick().

    When I use a script with left mouse button, the prefab instantiates like it should, and fires a rocket up in the air:

    Webplayer with mousebutton script ---> http://achimkinzelmann.homepage.t-online.de/Webseite/WebGL/Unity Pinguins/index.html

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class Raketenabschuss : MonoBehaviour
    7. {
    8.  
    9.     public Rigidbody Rakete;
    10.     public Transform barrelEnd;
    11.     public float thrust;
    12.  
    13.     void Update()
    14.     {
    15.         if (Input.GetMouseButtonDown(0))
    16.  
    17.         {
    18.  
    19.             Rigidbody RaketenInstance;
    20.             RaketenInstance = Instantiate(Rakete, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
    21.             RaketenInstance.AddForce(transform.forward * thrust);
    22.         }
    23.    
    24.     }
    25. }
    26.  
    ____________
    But when I use almost the same script with the UI button onClick(), then the rocket doesnt go up in the air, but it instantiates sideways in z-direction, no matter what I am trying to change:

    Webplayer with UI Button ---> http://achimkinzelmann.homepage.t-online.de/Webseite/WebGL/UI Button/index.html

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ButtonFire : MonoBehaviour
    7. {
    8.  
    9.     public Rigidbody Rakete;
    10.     public Transform barrelEnd;
    11.     public float thrust;
    12.  
    13.     public void RaketeAbfeuern()
    14.  
    15.     {
    16.    
    17.             Rigidbody RaketenInstance;
    18.             RaketenInstance = Instantiate(Rakete, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
    19.             RaketenInstance.AddForce(transform.forward * thrust);
    20.  
    21.     }
    22. }
    23.  
    Why does it instantiate in z-direction when I use the UI button instead of the script with the mouse ? And how can I fix it ?
     
    Last edited: Nov 26, 2018
  2. Julien_at_work

    Julien_at_work

    Joined:
    Aug 9, 2018
    Posts:
    30
    It's most likely because those two different transforms are rotated differently.
    And in AddForce you're using the owning transform, maybe you intended to use the barrel-transform?
     
    Achim likes this.
  3. Achim

    Achim

    Joined:
    Dec 19, 2007
    Posts:
    199
    I tried and it is now using the Rect Transform of the UI button itself. Thats why it is starting like this now. When I rotate the UI button, then the rocket is starting in this rotated direction too. How can I use the transform from the gameobject from where the rocket starts and not the UI buttons transform when using the UI button ?
     
    Last edited: Nov 26, 2018
  4. Julien_at_work

    Julien_at_work

    Joined:
    Aug 9, 2018
    Posts:
    30
    Yes, that's what i said, in different words.
    Assign the transform that you want to use to a variable (i guess for you this should be public) and pass that into the AddForce method.

    Or in german: Du musst das Ziel-Transform einer Variablen zuweisen, welche du der AddForce methode übergibst, 'transform' selbst referenziert nur das besitzende Transform.

    example:
    ...
    public Transform myTargetingTransform; //this could also be your barrelEnd //eventuell möchtest du aber auch das barellEnd nutzen, dann das weglassen
    ...
    RaketenInstance.AddForce(myTargetingTransform.forward * thrust); //use it here //hier benutzen (oder evtl. barellEnd, wenn eigentlich gewollt)
     
    Achim likes this.
  5. Achim

    Achim

    Joined:
    Dec 19, 2007
    Posts:
    199
    Great it works, thank you,
    Ich dachte es würde mit dem Script automatisch den Transform des anderen Gameobejct verwenden da es ja oben als public Transform definiert wird, Danke !:)
     
unityunity