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. Dismiss Notice

Moving/Instantiating Rect Transform Images in the U.I. as PREFABS & local vs world space?

Discussion in 'Scripting' started by dreg_master, Jan 17, 2017.

  1. dreg_master

    dreg_master

    Joined:
    Jan 4, 2016
    Posts:
    44
    Been playing with the UI for some time now. Unity ver 5.4.3f1
    Unable to successfully instantiate prefabs in in the UI with image and script components enabled and working.

    Untitled.png
    Then the original is destroyed and the prefab is created 0,0,1 dead center but not visible and components are unchecked
    Untitled2.png


    above the prefabs have instantiated (clone) with Image & scrips unchecked? why? also if i check them manually during playback the image it wont show. Sprites will show sometimes but are very hard to work with re x,y cords I've found cause i don't know how to convert properly i am assuming. I am also Assuming its because no matter what i try they JUST WONT instantiate under the canvas or the empty gameobject under the canvas, and that is why i cant see them. The script component will work and move the transform, its just basically invisible, but the original is always see-able (if that's even a word)?

    All I'm trying to do is move a sprite or image left or right a certain amount till it exits the screen and removes itself and instantiates another one behind it (well 2 spaces behind it as ill have another 2nd Prefab once i figure this out ill have 2 prefabs removing and instantiating, across the sky in the background to create a scrolling cloud effect in the UI using current rect transforms (of which anchors and alignment and image pivot points and arrghhh..) and UI component images. I have been able to do this outside of using the UI (game play not main menu) just using transforms and Sprites on layers. Others have suggested using a different camera.? I really not sure if i should tackle this as a canvas sprite or canvas image? or forget the UI all together and simulate my own canvas stuff?? (i have Italic the NOTES)
    _____________________
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class CloudScroller : MonoBehaviour
    7. {
    8.     public float speed;
    9.  
    10.     //public RectTransform targetPositionR;
    11.     public Vector3 targetPosition;
    12.     public GameObject Prefab;
    13.     public GameObject theCanvas;
    14.    //public Transform parent;
    15.  
    16.  
    17.     void Start()
    18.     {
    19.         Prefab.transform.SetParent(theCanvas.transform);
    20.         //rectPos = transform.position;
    21.         //startPosition = this.transform.position;
    22.         //startPosition = this.
    23.         //GameObject go = (GameObject)Instantiate(Resources.Load("BackDrop2")) as GameObject;
    24.        // GameObject go = Instantiate(Prefab, Prefab.transform.localPosition, Prefab.transform.localRotation) as GameObject;
    25.  
    26.     }
    27.  
    28.  
    29.     void Update()
    30.     {
    31.         //targetPosition = targetPositionR.localPosition;
    32.  
    33.         MoveTowardsTarget();
    34.  
    35.     }
    36.  
    37.    //move towards a target at a set speed.
    38.      void MoveTowardsTarget()
    39.     {
    40.         //move towards the center of the world (or where ever you like)
    41.         //Vector3 targetPosition = new Vector3(200, 0, 0);
    42.         Vector3 currentPosition = this.transform.localPosition; // or just position
    43.  
    44.         //if (Vector3.Distance(currentPosition, targetPosition) > 3f)
    45.         Vector3 directionOfTravel = targetPosition - currentPosition;
    46.         //now normalize the direction, since we only want the direction information
    47.             directionOfTravel.Normalize();
    48.             //scale the movement on each axis by the directionOfTravel vector components
    49.             Debug.Log(currentPosition.x);
    50.  
    51.  
    52.         if (currentPosition.x <= targetPosition.x) stage2();
    53.  
    54.             this.transform.Translate(
    55.                 (directionOfTravel.x * speed * Time.deltaTime),
    56.                 (directionOfTravel.y * speed * Time.deltaTime),
    57.                 (directionOfTravel.z * speed * Time.deltaTime));
    58.         return;
    59.     }
    60.  
    61.  
    62.     void stage2()
    63.  
    64.     {
    65.         Destroy(this.gameObject); //my line
    66.         //Prefab.transform.parent = theCanvas.transform;
    67.         Instantiate(Prefab, new Vector3(0, 0, 1), Quaternion.identity);
    68.         Prefab.transform.SetParent(theCanvas.transform);
    69.     }
    70.  
    71. }
    72.  
    other attachment is just my history trying to research this before posting
    ididtry.png
     
    Last edited: Jan 18, 2017
  2. dreg_master

    dreg_master

    Joined:
    Jan 4, 2016
    Posts:
    44
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. dreg_master

    dreg_master

    Joined:
    Jan 4, 2016
    Posts:
    44
    Last edited: Jan 18, 2017
  5. dreg_master

    dreg_master

    Joined:
    Jan 4, 2016
    Posts:
    44
    Maybe a UI ScrollRect?
    https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-scroll-rect
    I know my answers aren't as good as a community of seasoned pros tho..
    Im going to try it out see if it works and if it does ill post result here.

    ..later... Nup thats not working either. Cant find anything on changing the offset of a rect transforms image at this stage
     
    Last edited: Jan 18, 2017