Setting a Transform - Not Working

I have what is probably an extremely basic question, but I’m having trouble getting this to work. I’ll start with my code:

void TaskOnClick()
   {

      
       if (InitiativePosition == 0)
       {
           MyDestination = gameController.InitiativeDestination[gameController.InitiativeDestination.Length - 1];
       }
       else
       {
           MyDestination = gameController.InitiativeDestination[InitiativePosition - 1];
       }

       Debug.Log("My Name is:" + nameText.text +  "and my position is: "+transform.position+" and my destination is: " + MyDestination);


       CardMove();
       InitiativeChange();
   }

void CardMove()
   {
       transform.position = MyDestination;
   }

What it’s meant to do is move this ‘card’ game objects around when I push a button. Their destination is determined by a vector3 array from a different script. I’ve made sure that array is actually sending over the right vector3, and it’s printing that console debug message with everything exactly how I want it, but it just seems like the CardMove function isn’t doing anything. I’m sure there’s a super simple answer but I haven’t been able to figure it out.

It may sounds silly, but Debug.Log the transform.position before and after line 23 to convince yourself this is not true.

Aside from that:

  • Are you certain you are moving the correct object? You’re printing out “nameText.text”, but is “nameText” attached to the same object as this script? Which GameObject is this script attached to (more than one in the scene? A parent or child of the object you actually want to move perhaps?
  • Are there any Animators attached to this object or any of its parents/children?
  • Are there any other scripts that might move this object in the scene?
1 Like

Thanks for taking the time to respond! So far here’s what I’ve found:

So when I put another debug like you said that seems to indicate that the transform is changing but honestly I’m really confused by this. When I’m looking at the transform of the game object in the editor when I push the button it isn’t changing.

  1. I’m pretty sure I am. It’s attached to an empty game object which is a parent to all the sprite and text elements that make up this ‘card’. If I move that empty object around the children should follow right? That nameText variable is declared in the same script, I attached it via making a public text and dragging the text in the unity editor.

There are multiple objects in this scene with the same script. The plan is for them to be receiving different myDestination vector3s, which is what the if // else section of my code is doing.

  1. They do have animators. There are animation triggers that cause other stuff to happen, but I’m not triggering any of them in this code so I didn’t realize that might mess with this thing.

  2. No other script is moving the object.

If you have any more guidance I would really appreciate it!!!

Yep!

So your code definitely is moving the thing! That means means one of two things:

  • Something else is moving the object right back to where it was after you change it
  • You’re moving the wrong object

This could very well be the culprit. Try disabling the animators for a sec and test this script again. Animators are known to overwrite transform data.

1 Like

MM yeah that’s it, it’s working with the animators disabled. It seems to be looping the ‘base’ animation and returning it to where it was perhaps.

Is there a way to maybe stop the base from looping, or some other way to keep the animator without interfering with moving the transform around when this button gets pushed?

Thanks again!

So the primary way to make the animator not interfere is for the animator to not be animating the same fields that your other scripts are changing. May I ask what your animator is animating exactly? I ran into a similar issue with an animator recently and my problem was that my animator was animating the transform of the “root” object in the hierarchy for the particular object instead of the child transform that I should have been animating.

As long as your animator isn’t animating the parent transform, you can freely move the parent from scripts without the animator interfering.

Let me show you with pictures…

So I have this button. It consists basically of an empty parent object with an animator on it, and then a child cube that is the actual visual button:

6209097--682152--upload_2020-8-15_23-33-19.png

The animator just animates that child cube up and down. When I originally set up the animator, I accidentally animated the transform of the root/parent object: The “Big Red Button” object. What I should have been animating was the “Cube” child object!

It’s easy to get confused in the Animation clip editor. Notice when you click Add Property you can select which property to animate. I accidentally chose that top “Transform” the first time, and that’s the Big Red Button root object transform. What I needed to do was go into Cube → Transform:

6209097--682155--upload_2020-8-15_23-36-9.png

Sorry for the awful huge screenshots.

Mm understood, thanks so much for taking the time to explain!

I’ll have to do a little thinking to apply it to my current situation. I have theses cards that represent different units, and they rearrange themselves based on turn order (that’s what the initiative order thing in my script is). The one that’s going next moves to the top of the screen, and the one that just went goes back to the bottom. The sticking point is that there’s another animation where the card expands to fill the whole screen so there’s enough room for you to read all the rules for that card. I was animating both the parent object with the script, and the card’s background sprite to achieve that effect, but I’ll have to come up with a way to do it that doesn’t involve moving the parent object from the sound of it.

The reason I’m not just doing it all in animations is because I want it to be easy for the game to operate with any number of units in the current game state, and it just seems like it’s going to be complicated to make animations to achieve that effect without it being a huge spider-web-like mess in the animator haha

1 Like