Refer and update other objects position in an unlinked script.

Hello everyone!

I’m struggling with this thing right now, which is refering to another objects position and changing the other objects position. Im learning how to change another objects position in a script, without having the script attached. I’ve managed to teleport it directly to the object the script is attached to, but I fail to make it move based on its own position. So the GameObject in Unity is called “Pineapple”, and my reference is the “myotherobject”.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class player_movement : MonoBehaviour
{

  public float speed=10f;
  public Transform myotherobject;



    // Start is called before the first frame update
    void Start()
    {
    myotherobject = GameObject.Find("Pineapple").GetComponent<Transform>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {

      float leftandright = Input.GetAxis("Horizontal");
      float upanddown = Input.GetAxis("Vertical");

        if (Input.GetKey("space"))
        {
      
          myotherobject.transform.position = myotherobject.transform.position + new Vector3(leftandright * speed * Time.deltaTime, upanddown * speed * Time.deltaTime, 0);

          Debug.Log(myotherobject.transform.position);
        }
    }
}

I can only get the position of the object the script is attached to. How should I write in order to get the current position of “Pineapple”? Any help is greatly appreciated. :slight_smile:

What is happening exactly? Is it throwing a null reference? Is it just not moving? It sure looks like if it found Pineapple, it would move it…

It’s not moving at all. Positions are not updated but just at the same place. Is it somewhere wrong in the getaxis-part? Like maybe only finds the axis of the objects the script is linked to, not the other object. But I dont know how to write to get axis of another object. :stuck_out_tongue:

Some more ideas:

Change it to look for “FOOBAR” and make sure you get an error. If you don’t, then maybe you’re not finding Pineapple. Remember, if you Instantiate Pineapple, it won’t be called Pineapple anymore. It will be called “Pineapple (Clone)” or something

Make a NEW empty cube object and call it ‘Rhubarb’ and then change your code to search for ‘Rhubarb’ and move it. Does that move? If so it’s something to do with your Pineapple, maybe another script on it preventing it from moving.

And I assume you ARE seeing the log information produced online 31 above?

Well I think it found Pineapple due to the fact that I was able to teleport Pineapple to the object the script is on.

It seems to find Rhubarb as well, but it’s not moving. Yep, I see the coordinates on line 31.

Is it something with
float leftandright = Input.GetAxis(“Horizontal”);
float upanddown = Input.GetAxis(“Vertical”);

should I update it to something like float leftandright = myotherobject.Input.GetAxis(“Horizontal”); (note that I don’t know how to write this particular line).

No, your inputs are fine. Input is a static class, which means there’s only one global notion of it. You can even print out the xmove/ymove contents, see what they are.

I recommend maybe breaking apart that crazy-long line 29 above:

make a temp variable that is the motion inputs
multiply it by the speeds
multiply it by Time.deltaTime
then add it to the position

In between every one of those things you can Debug.Log() what their values are and get a handle on what is going on.

You may find out that your input system is broken and giving you (0,0), which means now you don’t have to debug anything else and it has nothing to do with “another object.”

Its always best it break it out into small baby steps. The compiler is (generally) smart enough to produce precisely the same code, and now your code actually communicates what it is doing, and can be debugged with ordinary debug statements. Also, two years from now when you dig this up you will have a WAY better chance of understanding what that line does if it is all separate little baby steps.

And finally, you can even try a right-click-reimport on one of your scripts, just in case the compiler magically stopped compiling because of some error (happens from time to time).

Hm, interesting! I managed to move it by just random variables applied in the vector3, so it’s like you said. Something is amiss in the Input.GetAxis. I’m thinking that maybe it just tries to get the GetAxis from the object the script is in (the script is attached to an object called Stone, and from there I try to control Rhubarb).

These terms I have for explaining names for objects sounds like a game of Harvest Moon or something. ^^. I’m sorry for that.

Like I said, the Input class is static, so it doesn’t really care where you call it from, as long as its the main thread.

If you just make a Debug.Log() call that outputs what you’re getting back from Input.GetAxis( “Horizontal”), does it return nonzero values? If it is always zero then something is wrong with your InputManager setup. To fix this easily, make new project, copy over the InputManager.asset file from the new project. Be sure to back things up before you tinker too aggressively, or be using source control.

Ah, sorry I’ve been late for replying. It’s been a busy week. :). I will try this and come back to you as soon as I can! :slight_smile:

Update: I did check the output of the input.axis and as you said, it’s set as zero. So I will tinker a bit with inputmanager,

1 Like