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 AronChan · Nov 18, 2013 at 01:02 PM · c#colliderlistrequirecomponent

How do i access a SphereCollider added by RequireComponent?

Hi all

Im trying to access a SphereCollider that i added to my gameobject by using the RequireComponent. What i essentially want to do is set the colliders radius to my range variable as shown in the code below.

  using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [RequireComponent (typeof (SphereCollider))]
 public class tower : MonoBehaviour {
 
     public float towerRange;
     public float deployTime;
     public List<GameObject> towerTargets;
 
     // Use this for initialization
     void Start () {
         SphereCollider towerRangeCollider = this.GetComponent<SphereCollider>();
         towerRangeCollider.name = "Tower Range Collider";
         towerRangeCollider.radius = towerRange;
         towerTargets = new List<GameObject>();
     }
     
     // Update is called once per frame
     void Update () {
 
     }
     
     // Add enemies to the towers list of targets upon entering its range
     void OnTriggerEnter(Collider possibleTarget)
     {
         if (possibleTarget.tag == "Enemy" || possibleTarget.tag == "Friend")
         {
             towerTargets.Add(possibleTarget.gameObject);
             Debug.Log(possibleTarget.gameObject.name + " entered the trigger");
         }
     }
     
     // Remove enemies from the towers list of targets upon leaving its range
     void OnTriggerExit(Collider possibleTarget)
     {
         if (possibleTarget.tag == "Enemy" || possibleTarget.tag == "Friend")
         {
             for(int i = 0;i < towerTargets.Count; i++)
             {
                 if(towerTargets[i].gameObject.name == possibleTarget.gameObject.name)
                 {
                     towerTargets.RemoveAt(i);
                     i--;
                 }
             }
         }
     }
 }
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 GameVortex · Nov 18, 2013 at 01:25 PM

Anywhere within a function in your Tower class you can access the SphereCollider by doing this:

 SphereCollider sphereCollider = GetComponent<SphereCollider>();

You can use that in the start function to apply the radius.

To your side note question:

The OnTriggerExit will not be called at all because it has to be written with a upper case 'O' instead of a lower case 'o'.

Also you should not remove objects from a List inside of a foreach through that same list, because the foreach will then fail on the potential next iteration in the list. I usually do it like this if I need to remove from a list I am iterating through:

 for(int i = 0; list.Count; i++)
 {
     if(shouldBeRemoved)
     {
         list.RemoveAt(i);
         i--;
     }
 }
Comment
Add comment · Show 14 · 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 AronChan · Nov 18, 2013 at 01:27 PM 0
Share

... That was.. embarrasing. Thanks for the tip on the foreach loop :)

avatar image GameVortex · Nov 18, 2013 at 01:47 PM 0
Share

No problem =)

avatar image AronChan · Nov 18, 2013 at 02:30 PM 0
Share

About the SphereCollider. It just doesnt seem to pick up the changes i do on my start()

Ive updated the question if you wouldnt $$anonymous$$d taking a quick look at it.

avatar image GameVortex · Nov 18, 2013 at 02:45 PM 0
Share

It should work fine as far as I can tell. When and to what do you set the value of towerRange?

Also, when you say it does not pick up the changes, do you mean that it is created with a SphereCollider with default radius of 0.5 and it does not change?

avatar image AronChan · Nov 18, 2013 at 02:54 PM 0
Share

the goal is that whenever the prefab of the "tower" is instantiated it will attain the spherecollider and radius of the range attribute. atm i just have a cube with the script attached for testing purposes. but it should be able to change its radius whenever the radius attribute is changed.

Show more comments

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

18 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

Related Questions

Can't get a laser working properly. 2 Answers

My Project doesn't work - Generic.List throws NullReferenceException ?! 0 Answers

Problems with crouching (FPS/C#) 1 Answer

Detecting OnMouseDown from colliders in an array 1 Answer

Unable to fill list. 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