I Am Curious About 2d Object Prefab Movement. Please Somebody Help Me.

public class RollDice : MonoBehaviour
{
    public int DiceNumber = 0; //dice number
    public Text DiceText; //show dice number
    public Transform _target1; //dice position
    public Transform _target2; //dice position to move forward
    public int _GruondIndex = 0; //The ground array index that will move the empty game objects
    public float speed = 1f;
    /*public Transform horseSpwanpoint;//horse is _target1 prefab
   
    private void Awake()
    {
        Instantiate(_target1, horseSpwanpoint.position, horseSpwanpoint.rotation);
    }
    */
    private void Start()
    {
        _target2 = Tower.points[0]; // point is the location of the empty game objects in an array.
    }
    void Update()
    {
        _target1.position = Vector2.MoveTowards(_target1.position, _target2.position, speed * Time.deltaTime);
    }


    public void Dicegogo()
    {
        int ran = Random.Range(1, 6);
        this.DiceNumber = ran;
        DiceText.text = "Value : " + DiceNumber.ToString();
        if (DiceNumber == 6)
        {
            _GruondIndex += 6;
            _target2 = Tower.points[_GruondIndex];
        }
        else if (DiceNumber == 5)
        {
            _GruondIndex += 5;
            _target2 = Tower.points[_GruondIndex];
        }
        else if (DiceNumber == 4)
        {
            _GruondIndex += 4;
            _target2 = Tower.points[_GruondIndex];
        }
        else if (DiceNumber == 3)
        {
            _GruondIndex += 3;
            _target2 = Tower.points[_GruondIndex];
        }
        else if (DiceNumber == 2)
        {
            _GruondIndex += 2;
            _target2 = Tower.points[_GruondIndex];
        }
        else
        {
            _GruondIndex += 1;
            _target2 = Tower.points[_GruondIndex];
        }
        Debug.Log(_target2);
    }       
}

This coding will work. But if i call awake method, it will not work.
I wonder if there is a rule that should not be used together, or if the coding is wrong.

Thank you for reading. :slight_smile:

whats the error?

If i call awake method, _target1 will be created. But it will not move.

There is no response.

Note that : This class is dependent on the GUI.

i guess that is happening because you want to move the literal prefab you dragged in hiearchy.
this as a field

Transform cloneTarget;

this to awake

cloneTarget = (Transform) Instantiate(_target1, horseSpwanpoint.position, horseSpwanpoint.rotation);

and this in update

cloneTarget.position = Vector2.MoveTowards(_target1.position, _target2.position, speed * Time.deltaTime);

you would limit this class instance to have only one clonetarget though.

1 Like

OMG. It works!!!

Thank you for your interest. :wink: Thank you soooo much!!

Can I ask you one more question?

" (Transform) Instantiate " , Is there a way to specify this as a single target?

not sure what you mean by ā€œsingle targetā€.

I donā€™t know why I write " (Transform) Instantiate " . you said that limit this class instance to have only one clonetarget though.

So I think it is like "only one clonetarget " and ā€˜singletargetā€™.

Is there a difference between doing " (Transform) Instantiate " and doing "Instantiate " ?

Well you can of course have public Transform cloneTargets[ ] as a array, so you could have more those targets.
instantiate them in awake like cloneTargets[0] = (Transform) Instantiate(_target1, horseSpwanpoint.position, horseSpwanpoint.rotation); Or all of them with a loop.
and move them in update each one separately like clone Targets[0].Vector2.MoveTowards();
or all at one within a loop.
about that (Transform) Instantiate, i think you have to cast it so its the same type as the original, its the same as writing " as Transform" in the end.
i hope what i said isnt nonsense im not magister myself :- )

Out of curiosity - your code seems unneccessarily convoluted - why did you write

public void Dicegogo() {
        int ran = Random.Range(1, 6);
        this.DiceNumber = ran;
        DiceText.text = "Value : " + DiceNumber.ToString();
        if (DiceNumber == 6)
        {
            _GruondIndex += 6;
            _target2 = Tower.points[_GruondIndex];
        }
        else if (DiceNumber == 5)
        {
            _GruondIndex += 5;
            _target2 = Tower.points[_GruondIndex];
        }
        else if (DiceNumber == 4)
        {
            _GruondIndex += 4;
            _target2 = Tower.points[_GruondIndex];
        }
       else if (DiceNumber == 3)
       {
            _GruondIndex += 3;
            _target2 = Tower.points[_GruondIndex];
        }
        else if (DiceNumber == 2)
        {
            _GruondIndex += 2;
            _target2 = Tower.points[_GruondIndex];
        }
        else
        {
           _GruondIndex += 1;
           _target2 = Tower.points[_GruondIndex];
        }

        Debug.Log(_target2);
    }

when the following would do the same

public void Dicegogo() {
        int ran = Random.Range(1, 6);
        this.DiceNumber = ran;
        DiceText.text = "Value : " + DiceNumber.ToString();
        _GruondIndex += ran;
        _target2 = Tower.points[_GruondIndex];

        Debug.Log(_target2);
 }

-ch

Ohā€¦ Iā€™m so stupid. Thanks :slight_smile:

ohā€¦ So is ā€œ(Transform) Instantiateā€ the same effect as ā€œas Transformā€?

The reason I did not work the first time, because I didnā€™t write code it to get this transform?

no , the reason it didnt work was because you were trying to drive a car in showroom, just by holding and turning a paper ad for that car. That is ,you were trying to move the actual prefab you have in your project in assets so to speak.not the gameobject in the game.
and that (Transform) or ā€œas Transformā€ ā€¦if you ,just for trying, were to delete that i think it will be an error or it would not work. Or it just might work i am not sure.but in general i think it has to be there : - )

Thank you for your kind reply.

Thank you for your kind reply.

If i use different asset, can i use (getsomething)?

I understand about prefab and gameobject.

I know it makes sense.

Again, Thank you.