- Home /
Semi-Quick question about "LookAt" script after respawn.
Hi,
I am having a problem with my "Look at" type of script(JavaScript). It's on a boat turret and it fires when it spawns as long as it can find the target tagged BoatTarget. It makes the turret on the boat look at the target as long as the target hasn't been destroyed yet but as soon as the target gets destroyed and respawns the boat turret wont look at the target or fire anymore. I have a similar script on an enemy itself that works just fine, which leads me to believe that the target isn't properly attatched to the prefab of the boat in the inspector(I thought it was in script but after the target respawns it doesn't do anything). I can't even assign it in the inspector because the prefab parent that the target is a child under doesn't have a drop down arrow. I did it in the scene view (when "target" was a public variable) because thats the only place I could do it other than code and it doesn't work there either. Here is the script im using:
private var target : GameObject;
var boatProjectile : GameObject;
var boatFp1 : GameObject;
var nextFire : float = 1;
var fireRate : float = 1.1;
function Update ()
{
target = GameObject.FindWithTag("BoatTarget");
if(!target)
{
return;
}
transform.LookAt(target.transform.position);
if(Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(boatProjectile, boatFp1.transform.position, boatFp1.transform.rotation);
}
}
I would like an example of how to assign it or a work around if anyones got one rather than a sriaght answer so I can learn what I am doing wrong rather then do it and forget. But if thats all you got I wont turn it down. Thanks for the help!
Edit: The problem is that the target that is respawning is a clone that doens't have everything on it that the one in the scene view does. How do i fix this if I already hit apply and drag and dropped the target back into the inspector prefab and it didn't change the clone?
Can anyone give me a hint? Im not even getting any errors.
I am going to Bump this one more time so hopefully somebody has an answer, if not oh well I guess.
Extra info: I am having similar problem with sound, it wont let me assign my plane(which is the player) to this gun script below, well it will but at soon as I respawn I get a null reference error because the assigned object is now gone on the clone. Just thought this info might help solve my original question then lead to answering this. Here is the error:
UnassignedReferenceException: The variable fireSource of 'Plane01Guns' has not been assigned. You probably need to assign the fireSource variable of the Plane01Guns script in the inspector. UnityEngine.GameObject.GetComponent (System.String type) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:20) Plane01Guns.Start () (at Assets/Scripts/Plane01Guns.js:15)
#pragma strict
var projectilePrefab : GameObject;
var fp1 : GameObject;
var fp2 : GameObject;
var fireRate : float = 0.2;
var nextFire : float = 0.2;
private var fireSound : AudioSource;
var fireSource : GameObject;
function Start()
{
fireSound = fireSource.GetComponent("AudioSource");
fireSound.enabled = false;
}
function Update ()
{
if( Input.GetButton("Fire1") && Time.time > nextFire)
{
fireSound.enabled = true;
GunFireSound();
nextFire = Time.time + fireRate;
Instantiate(projectilePrefab, fp1.transform.position, fp1.transform.rotation);
nextFire = Time.time + fireRate;
Instantiate(projectilePrefab, fp2.transform.position, fp2.transform.rotation);
}
}
function GunFireSound()
{
audio.Play();
audio.volume = 0.3;
audio.loop = false;
}
Answer by gribbly · Mar 16, 2013 at 06:12 AM
Are you sure the respawned target is getting tagged with "BoatTarget"?
You should be able to check in the inspector while the game is running (e.g., don't maximize the game window, select the target and see if it's tagged).
You probably need to do something like (in whatever script spawns new enemies):
var enemy:GameObject;
enemy = Instantiate(enemyPrefab, spawnPosition, spawnRotation);
enemy.tag = "BoatTarget";
Your right, I just had a similar problem with the audio. When the player("target") dies and a "clone" instantiates or respawns in its place it doesn't have some things that it should. I tried bringing (and "applying") the player prefab back into the inspector from the scene but it stil instantiates a clone without certain things on it (i.e. the tag of ("boat target"). how should I go about fixing this? the same way or is that a fix to a different problem.
Try manually dragging the prefab into the editor (when the game is not running) and taking a look in the inspector. Is everything set up correctly?
If not, the problem is with the prefab. Set it up exactly right in the editor window, then drag and drop it on the prefab to ensure it's set up correctly.
Otherwise, I don't know I'd have to see your spawning code.
Ok I tried dragging it in but it was the same as in the scene so nothing different here is the respawn code I put together:
private var player : GameObject;
var lives : int = 3;
var playerspawnPoint : GameObject;
var playerOne : GameObject;
var respawnStarted : boolean = false;
function Update()
{
player = GameObject.FindWithTag("Player");
if(!player && !respawnStarted)
{
RespawnInstructions();
}
}
function RespawnInstructions()
{
respawnStarted = true;
lives--;
if(lives <= 0)
{
yield WaitForSeconds(3.5);
Application.LoadLevel("Aliens$$anonymous$$ain$$anonymous$$enu");
}
else
{
yield WaitForSeconds(2);
var newPlayer : GameObject = Instantiate(playerOne, playerspawnPoint.transform.position, playerspawnPoint.transform.rotation);
newPlayer.transform.parent = Camera.main.transform;
}
respawnStarted = false;
}
Hmmm, I don't use javascript (I do everything in C#), but try moving this line:
var fireSource : GameObject;
...before any "private var" declarations. It could be that fireSource is private, that's why you can't assign it and are getting that null reference exception.
For the original, I would recommend confir$$anonymous$$g whether "target" is actually null when the player is respawned. Something like:
target = GameObject.FindWithTag("BoatTarget");
if(!target)
{
Debug.Log("Target is null!");
return;
}
Do you see "Target is null!" in the console when the problem occurs?