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

Unable to change the position of a sprite

Discussion in '2D' started by KunoNoOni, Sep 16, 2014.

  1. KunoNoOni

    KunoNoOni

    Joined:
    Nov 16, 2013
    Posts:
    27
    What would seem like a very trivial thing has become a hair pulling nightmare.

    I have a sprite, a selector box, which follows the mouse around the screen. It has a script which is attached to an empty gameobject in the scene. I also have a player sprite which also has a script which is attached to an empty gameobject, called PlayerManager, in the scene. Now my player script has a public gameobject in it which holds my player sprite. The variable for this player sprite is called pShip. So here is what I am trying to do, I want the player sprite to move instantly to the location of the selector box when the left mouse button is clicked.

    Sounds simple enough.

    In my script for the selector box I check to see if the left mouse button was clicked. My question is this, to access the transform.position of the player sprite should I do this:

    Code (CSharp):
    1.  
    2. Player player;
    3.  
    4. player = GameObject.Find("PlayerManager").GetComponent<Player>();
    5. player.transform.position = this.transform.position;
    6.  
    or like this:

    Code (CSharp):
    1.  
    2. Player player;
    3.  
    4. player = GameObject.Find("PlayerManager").GetComponent<Player>();
    5. player.pShip.transform.position = this.transform.position;
    6.  
    I've tried both but neither move the player sprite to the location of the selector box. What am I doing wrong?

    -KunoNoOni
     
    Last edited: Sep 16, 2014
  2. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    You need to learn a bit more about C# generics.

    GetComponent<T> is the generic method for getting a component by it's Type, where T is the Type (not a string, as you have provided).

    Also, there shouldn't be a dot between the method name GetComponent and the <> brackets. Dots denote a member of the previous item like this.transform.position says this's transform's position element.

    So, assuming you have a component in the PlayerManager object of Type Player, write it like this:

    player = GameObject.Find("PlayerManager").GetComponent<Player>();

    If that doesn't make sense, read up on how Game Objects and Components work in Unity, and how Types work in C#.
     
  3. KunoNoOni

    KunoNoOni

    Joined:
    Nov 16, 2013
    Posts:
    27
    This is what happens when you try to do things from memory :). I actually do have them like that. I will edit my original post. thank you for pointing that out.
     
  4. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    Is the Player an object in the game world or a component on the Player Manager? Is it a component in an object that is a child of the Player Manager? If that is the case, you meant GetObjectsInChildren (I think thats what its called).

    Lastly, is the Sprite component attached to the same Game Object as the Player component? If not, then changing the player object's transform won't move the sprite.
     
  5. Punchbag

    Punchbag

    Joined:
    Oct 30, 2012
    Posts:
    48
    [Edit] Re-read what you're working on and realised my mistake about which place the script is in.
     
  6. KunoNoOni

    KunoNoOni

    Joined:
    Nov 16, 2013
    Posts:
    27
    To answer your first question, I'm not sure.

    My player sprite is an Object, which I created a Prefab for. In the Player script I instantiate it into the gameworld. If I look in the Hierarchy I can see player(clone) exists. Like I mentioned above, my Player script has a
    Code (CSharp):
    1. public GameObject pShip;
    where I have linked the player Prefab to.

    I don't believe its a child, but I will try that when I get home tonight.

    I'm going to have to look. I will get back to you.
     
  7. KunoNoOni

    KunoNoOni

    Joined:
    Nov 16, 2013
    Posts:
    27
    Punchbag I do want to thank you for your assistance. I took a long hard look at what I was doing and came at it from a different angle. I removed everything from the Player.cs script and added the script to the player Prefab, I then instantiated the pShip from the SpacePorts script and now the movement code works when I click the left mouse button.

    What helped was when I tagged the player sprite with "Player" and now I search for it with GameObject.FindWithTag("Player"); This give me access to the actual player sprite. To be honest I'm not sure what I was accessing before.

    Now to continue on with my game!