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 Frazzle402 · Jun 14, 2020 at 12:35 AM · scripting problemlistcoroutinebug-perhaps

1 out of 4 objects with the same script that reference the same transform list returning null?

Hi, i have 4 Gameobjects (turrets) with this same script attached which use a list of Transforms as targets. The Coroutine TargetSystem first checks if the list has a count > 0 "if (etm.targets.Count > 0)" or else "lockedBlocked = null; StopCoroutine(TargetSystem());" etc.

The issue im having is 3 out of the 4 turrets are passing the if count > 0 and 1 is returning the else = null?!

 public class TurretController : MonoBehaviour
 {
     private EnemyTargetManager etm;
     [HideInInspector] public Transform lockedBlock = null;
     public XRLineRenderer beam1;
     public float fireDistance = 300.0f;
     public float rayDistance = 300.0f;
     public float turnSpeed = 50.0f;
     private Quaternion targetRot;
     private bool targetAcquired = false;
     private ShipBlockColl blockColl;
     private AudioSource source;
     
 
     private void Start()
     {
         etm = FindObjectOfType<EnemyTargetManager>();
         etm = etm.GetComponent<EnemyTargetManager>();
         StartCoroutine(TargetSystem());
         source = GetComponent<AudioSource>();
     }
     
     private void Update()
     {
         if (targetAcquired == true && lockedBlock != null)
         {
             targetRot = Quaternion.LookRotation(lockedBlock.transform.position - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, turnSpeed * Time.deltaTime);
         }
     }
     
     IEnumerator TargetSystem()
     {
         //print("target started!");
         
         if (etm.targets.Count > 0)
         {
             //print("found target");
             if (etm.targets[0].CompareTag("ShipBlock"))
             {
                 lockedBlock = etm.targets[Random.Range(0, etm.targets.Count)];
                 Vector3 blockDistance = lockedBlock.position;
                 float dist = Vector3.Distance(transform.position, blockDistance); //Debug.Log(dist);
                 if (Vector3.Distance(transform.position, blockDistance) < fireDistance)
                 {
                     targetAcquired = true;
                     yield return new WaitForSeconds(1f);
                     StartCoroutine(FireBeam());
                     yield return new WaitForSeconds(5f);
                     targetAcquired = false;
                     yield return StartCoroutine(TargetSystem());
                 }
                 else
                 {
                     yield return new WaitForSeconds(1f);
                     StartCoroutine(TargetSystem());
                 }
             }
         }
         
         else
         {                                                            
             lockedBlock = null;                          // <------o but why?
             StopCoroutine(TargetSystem());
             print("LockedBlock Null!");
             yield return null;
         }
         
         if (etm.targets.Count < 0)
         {
             print("No target Count!");
             StopCoroutine(TargetSystem());
             lockedBlock = null;
             yield return null;
         }
     }
 
     IEnumerator FireBeam()
     {
         //print("Beam Fired!");
         source.pitch = 0.9f;
         source.Play();
         yield return new WaitForSeconds(0.80f);
         beam1.enabled = true;
         CastBeam();
         yield return new WaitForSeconds(2.5f);
         source.Stop();
         beam1.enabled = false;
         yield return null;
     }
 
     public void CastBeam()
     {
         RaycastHit hit;
         Vector3 direction = transform.forward;
         LayerMask mask = LayerMask.GetMask("ShipBlock");
         
         if (Physics.Raycast(transform.position, direction, out hit, rayDistance, mask))
         {
             //print("ray fired!");
             ShipBlockColl blockColl = hit.collider.GetComponent<ShipBlockColl>();
             //Debug.DrawRay(transform.position, direction * rayDistance, Color.red, 2f);
             
             if (blockColl != null && hit.collider.CompareTag("ShipBlock"))
             {
                 //print("beam hit!");
                 blockColl.RayBeamHit();
             }
             else
             {
                 Debug.Log("Did not Hit");
             }
         }
     }
 }

I have found a dirty workaround by copying and renaming the script just for this one turret and adding this to the else statement............

 else
         {                                                            
             lockedBlock = null;                          // <------o but why?
             StopCoroutine(TargetSystem());
             print("LockedBlock Null!");
             yield return new WaitForSeconds(2f);
             StartCoroutine(TargetSystem());
         }
 

After the Coroutine is started again it works fine :/

thanks.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Frazzle402 · Jun 14, 2020 at 01:47 AM

Fixed by myself by initializing my Transform list of targets in Awake instead of Start, i think the 4th turrets TargetSystem coroutine was running before the list was initializing hence the return to else = null.

Comment
Add comment · 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

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

236 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to remove an item from a list of custom variables 1 Answer

CustomRenderTexture (RFloat) wont initialize in the same frame it is created. 0 Answers

the player is not shooting 1 Answer

yield return new WaitForSeconds not working 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