Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by serenefox · Mar 16, 2013 at 05:32 AM · javascriptprefabinspectorlookat

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image serenefox · Mar 16, 2013 at 07:22 AM 0
Share

Can anyone give me a hint? Im not even getting any errors.

avatar image serenefox · Mar 16, 2013 at 08:15 AM 0
Share

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;
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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";
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image serenefox · Mar 16, 2013 at 06:20 AM 0
Share

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.

avatar image gribbly · Mar 16, 2013 at 06:24 AM 0
Share

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.

avatar image serenefox · Mar 16, 2013 at 06:28 AM 0
Share

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;    
 }
avatar image gribbly · Mar 16, 2013 at 08:23 AM 0
Share

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?

avatar image serenefox · Mar 16, 2013 at 08:32 AM 0
Share

Yes I do get that.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity fire1 prefab in code 2 Answers

Get a list of options for variable in the script inspector? 1 Answer

Custom Inspector Invocation behaviour? 1 Answer

Object reference not set despite assigning instance of an object in GUI 1 Answer

How to differentiate between prefabs on collision. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges