playMaker

Author Topic: How to store a prefab as an "Object" variable? [SOLVED]  (Read 2446 times)

velketor

  • Junior Playmaker
  • **
  • Posts: 69
  • Multimedia Marathon Man
    • Shawn Kilian Portfolio
How to store a prefab as an "Object" variable? [SOLVED]
« on: December 30, 2012, 05:00:14 PM »
**I already know the difference between GameObject variables and Object variables***

I spawn a player prefab.  When the player jumps into a pit, I destroy the player. 

When the player hits spacebar, I respawn the player prefab at the same location it was destroyed.

This is where I'm stumped.  I have a "follow" script (see below) on my enemy prefab.  The enemy normally follows the player with no problem...but after I've destroyed the player prefab 1 time, the script loses it's reference to the player prefab and the enemy no longer follows the player.  How can I make the script below keep it's reference to the player target? 

Code: [Select]
var target : Transform; //the enemy's target
var moveSpeed = 10; //move speed
var rotationSpeed = 310; //speed of turning

var myTransform : Transform; //current transform data of this enemy

function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
     target = GameObject.FindWithTag("aggro").transform; //target the player

}

function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;


}

I've already tried setting the "target" property of the script but it only allows me to use an "Object" variable.  I can't seem to get anything other than a script to sit in the "Object" variable.  Normally I just find a "GameObject" by tag and store it as a variable.  That doesn't work when dealing with external scripts though.  Any help is greatly appreciated.  Thank you.
« Last Edit: May 11, 2013, 01:55:15 PM by Alex Chouls »

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: How to store a prefab as an "Object" variable?
« Reply #1 on: December 30, 2012, 05:46:38 PM »
you can just create an object and then set its' type to Unity-> Transform (you can change the type only after the creation at the bottom of the variables window). Then use a getComponent action and use that on your player. It will automatically get a component of the type your object variable is. Then just pass it on to your script.
« Last Edit: December 30, 2012, 05:48:35 PM by kiriri »
Best,
Sven