- Home /
Player wont be found on start
My current script is a script for a door collision. when the Distance reaches less then 1 the player is moved to a separate scene and to the spawn point in which is specified in the code. I am currently having problems with the player not being found and when i just assign the player manually the send message script does not find the its way to parent in which the second script is attached. The game object Trigger which the first code is attached to is the child of the Spawnpoint in which the Second script is attached. Thanks in advance
this is my code:
var Player : Transform;
var Inside : float = 1;
var GoingToSpawnPNT : int = 0;
var Dist : float;
function Start()
{
Player = transform.Find("Player");
}
function Update()
{
if (Player != null)
{
var dist : float = Vector3.Distance(Player.position, transform.position);
Dist = dist;
if (dist < Inside)
{
Player = null;
transform.root.gameObject.SendMessage("Assign", GoingToSpawnPNT);
}
}
}
This is my Second code
var CustomSpawnPoint : int;
static var Spawn : int;
var Level : String;
function Update()
{
if (Spawn == CustomSpawnPoint)
{
Debug.Log("Spawned");
Spawn = 0;
//Create Character
}
}
function Assign(SpawnNum : int)
{
Spawn = SpawnNum;
Application.LoadLevel(Level);
}
Using static variables to keep values between level is a bad habit. Consider using the singleton pattern ins$$anonymous$$d.
Answer by fafase · Aug 30, 2012 at 05:22 AM
Well, I am a little confused on the hierarchy of your objects but my view would be to use a trigger instead of the first script and a reference to the script (I don't really fancy SendMessage but it is personal):
var GoingToSpawnPNT:int = 0;
var obj:cube ;
function Start(){
obj = GameObject.FindWithTag("Player").GetComponent(cube);
}
function OnTriggerEnter(other:Collider){
if(other.gameObject.tag=="Player")obj.Assign(GoingToSpawnPNT);
}
Sorry if i did not clarify the hierarchy well enough. There are 2 objects Trigger and SpawnPoint Trigger is the child to SpawnPoint.
As for changing it to a collision, i found that that didnt work how i had liked so im using distance.
Do you have any idea as to why player is not being found?
Your answer
Follow this Question
Related Questions
How to find a prefab in another scene? 1 Answer
Finding A Variable With A String Variable 1 Answer
How do I place a player in the right spot when I change scenes? 1 Answer
SendMessage script problem 2 Answers
Can't put Sendmessage to work. 1 Answer