My GameObject remains inGame but is destroyed in the editor (no GameObject on left side/in hierarchy)
An enemy is spawned after trigger is activated, he then rushes towards the player and the camera focuses its gaze on it. After destroying everything works fine, except the GameObject (which is deleted in the editor) is still glitching around upon my player...
Also, I'm getting a Refrence error although the camera stuff is working fine :
NullReferenceException: Object reference not set to an instance of an object ForceGaze.SetCam () (at Assets/Scripts/ForceGaze.js:10) ForceGaze.Update () (at Assets/Scripts/ForceGaze.js:19)
And here's my code where I guess something has to be wrong...any ideas? :) All done in Javascript btw.
ForceGaze.js:
var camLookAt : Transform;
var cameraUsed : Camera;
var Haunt : GameObject;
function SetCam()
{
if(PlayerSpawnTrigger.spawning == true)
{
camLookAt.position = GameObject.FindWithTag("Haunt").transform.position;
}
}
function Update()
{
SetCam();
if (GazeChecker.ForceGaze == true)
{
cameraUsed.transform.LookAt (camLookAt);
}
}
HauntSpawner.js:
var Player : Transform;
var spawnSound: AudioClip;
var MinSpawnPos : float = 5;
var MaxSpawnPos : float = 25;
var EnemyPrefab : GameObject;
var MinEnemies : int = 1;
var MaxEnemies : int = 1;
var HowManyToSpawn : int = 1;
static var functionActivated = false;
function Update()
{
///Spawn enemy on Trigger///
if(PlayerSpawnTrigger.spawning == true && functionActivated == false)
{
Spawn();
}
}
function Spawn()
{
var dist : float = Vector3.Distance(Player.position,transform.position);
if(PlayerSpawnTrigger.spawning == true)
{
GetComponent.<AudioSource>().PlayOneShot(spawnSound);
var position: Vector3 = Vector3(Random.Range(MinSpawnPos, MaxSpawnPos), 3.2006, Random.Range(MinSpawnPos, MaxSpawnPos));
HowManyToSpawn = 1;
for(var i = 0; i < HowManyToSpawn; i++)
{
HauntClone = Instantiate (EnemyPrefab,position,Quaternion.identity);
HauntClone.tag = "Haunt";
}
functionActivated = true;
}
}
HauntMoving.js:
var Player : Transform;
var MoveSpeed = 4;
var MinDist = 1;
var MaxDist = 15;
function Update()
{
///Moves toward Player////
transform.LookAt (Player.position);
var dist = Vector3.Distance(transform.position,Player.position);
if(dist <= MaxDist)
{
transform.position += transform.forward * MoveSpeed*Time.deltaTime;
}
if(dist <= MinDist)
{
Destroy(GameObject.FindWithTag("Haunt"));
PlayerSpawnTrigger.spawning = false;
}
if(PlayerSpawnTrigger.LeftTriggerZone == true)
{
Destroy(GameObject.FindWithTag("Haunt"));
}
}
Thanks you for any help!
// hi again tray :
function SetCam()
{
if(PlayerSpawnTrigger.spawning == true)
{
if(GameObject.FindWithTag("Haunt") != null)
camLookAt.position = GameObject.FindWithTag("Haunt").transform.position;
}
}
Alright, so the null reference error is gone - thank you very much! But I still have the problem, that my GameObject is deleted in the editor as soon as it reaches my Player (which is totally fine) but it is still in the game (there it does'nt vanish)...
genuinely i don't know but tray to delete on of the 2 scenarios that destroy the hunt to know which one of them do the job or both of them & make sure the hunt brefab is taged Hunt to make sure you are not destroying other object.
Alright, will do so tomorrow and let You know The outcome - thank You very much! :)
The HauntSpawner.js is attached to an empty GameObject in the world. The Prefab has the $$anonymous$$ovement.js attached to it, since it will not move towards the player because of the max Dist variable within the script - thats why I placed it way out of the game world.
But I needed to attach the movement script to the prefab, since the prefab clones will spawn WITHIN that max Dist and therefore move toward the player when triggered...
The problem is: when spawning a clone with the tag "Haunt", suddenly my Prefab spawns at the same location as well (although it does not have the tag "Haunt" and is not instantiated (its there beforehand, way off) after the trigger but teleported... :)
i am afraid you have tagged the empty GameObject as Hunt if so un tag it & don't tag the Prefab in script HauntClone.tag = "Haunt";
you can tag it in unity even it as a Prefab just click it in unity & give it a Hunt tag
Your answer
Follow this Question
Related Questions
Keep GameObject destroyed on returning to the scene 1 Answer
Information about when it is destroyed any GameObject on scene. 0 Answers
Framing multiple objects from a list along y axis only? 0 Answers
Instantiated bullet wont destroy 1 Answer
How to prevent duplicated gameObjects with disabled camera? 0 Answers