Search Unity

Instantiate objects that "stick"

Discussion in 'Scripting' started by yangmeng, Mar 1, 2007.

  1. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Is there an easy way to instantiate an object that sticks to the player? So that the player can pick something up and carry it around with it staying in view? Thanks.
     
  2. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    probably the simplest way would be:

    Code (csharp):
    1.  
    2. // instantiate the object
    3. myObject = Instantiate(prefab);
    4.  
    5. // make the object a parent of the player
    6. myObject.transform.parent = thePlayer.transform;
    7.  
    8. // if you want to, change the object's location to move somewhere else, like the player's back
    9. myObject.transform.localPosition = newLocalPosition;
     
    Nachoripan likes this.
  3. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    Awesome. Thank you.
     
  4. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
  5. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    I didn't have a chance to try this until now, but I am running into a problem.
    I thought the "(prefab)" in the script above would be a variable that a prefab is assigned to so I had this:
    Code (csharp):
    1. var key : Transform;
    2. var player : Transform;
    followed after other code by this:
    Code (csharp):
    1. myObject = Instantiate(key);
    2. myObject.transform.parent = player.transform;
    And now Untiy won't let me assign the prefab to that variable anymore. It only lets me assign something that is already in the game and the object I want to use is not yet in the game.
    Any idea what I am doing wrong?
    Thanks
     
  6. Samantha

    Samantha

    Joined:
    Aug 31, 2005
    Posts:
    609
    Both of the objects will need to be in the game in order to change the transform parent. A little more detailed explanation of what you're trying to do would be helpful :)

    Your variable declarations should be:
    Declaring the variables as Transforms, the trying to access the transform from within the transform is why Unity is getting confused. Try that out and see if it works.
     
  7. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    My game was broken for about a month and I have finally gotten it mostly fixed and can turn back to this issue.
    I am no longer instantiating the object I want to "stick" to the player. I now just need to have the player "carry" an object that it runs into. I tried making the object a child of the player with this script below but got the warning "An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.
    Code (csharp):
    1.     transform.parent = Player.transform;
    The script is attached to the object I want the player to carry. Any ideas?
     
  8. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    What is "Player"? Are you sure it's not 'player' or 'thePlayer' as above? It sounds like it thinks you're referring to a class (another script) called "Player."
     
  9. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    "Player" is actually the name of my player in the Hierarchy. Using this name works elsewhere in the scripts I am using but maybe this is a special case...
     
  10. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    is

    Code (csharp):
    1. var Player : GameObject;
    declared at the top of the script and assigned in the inspector?
     
  11. yangmeng

    yangmeng

    Joined:
    Dec 5, 2006
    Posts:
    573
    It is now... Thanks.