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 StigC · Mar 09, 2014 at 10:39 PM · c#getcomponentfindgameobjectswithtagmultiple-objects

Accesing components on multiple objects in c#?

Hi there.

What I'm trying to do is to have triggers in the scene that will allow the player to rotate the camera around him, only when he is inside one of those objects colliders.

I have multiple object that all have the same script attached (in this instance named RotateArea.cs).

And I want to access all those objects from another objects script component (CameraRotator.cs).

Is there a way in Unity to access all of those at the same time? I'm currently using FindGameObjectWithTag, as it gets an error as soon as I use FindGameObjectsWithTag. Appearently I can't use .GetComponent after a FindGameObjectsWithTag.

What to do?

CameraRotator

 using UnityEngine;
 using System.Collections;
 
 public class CameraRotator : MonoBehaviour 
 {
     public float rotateTime = 1.0f;
     
     private bool _isTweening = false;
     private CrunchPlatformColliders _cruncher;
     private DisablePlayer _disablePlayer;
     RotateArea rotateFunction;
     
     void Start () 
     {
         _cruncher = GetComponentInChildren<CrunchPlatformColliders>();
         GameObject player = GameObject.FindGameObjectWithTag("Player");
         _disablePlayer = player.GetComponentInChildren<DisablePlayer>();
         rotateFunction = GameObject.FindGameObjectWithTag("rotateZone").GetComponent<RotateArea>();
     }
     
     
 
     void Update(){
         if(rotateFunction.isInRotateZone)
         {
             if (Input.GetKeyDown(KeyCode.Z))
             {
                 rotateTween(90);
             }
             
             if (Input.GetKeyDown(KeyCode.X))
             {
                 rotateTween(-90);
             }
         }
     }
 
     private void rotateTween(float amount) 
     {
         if (_isTweening == false) 
         {
             _isTweening = true;
 //            _cruncher.setPlayerPos();
             _disablePlayer.disable();
             Vector3 rot = new Vector3(0,amount, 0);
             iTween.RotateAdd(gameObject, iTween.Hash(iT.RotateAdd.time, rotateTime, iT.RotateAdd.amount, rot, iT.RotateAdd.easetype, iTween.EaseType.easeInOutSine, iT.RotateAdd.oncomplete, "onColorTweenComplete"));
         }
     }
     
     private void onColorTweenComplete()
     {
         _isTweening = false;
         _disablePlayer.enable();
         _cruncher = GetComponentInChildren<CrunchPlatformColliders>();
         //_cruncher.crunchCollidersToPlayer();
     }
 }



RotateArea

 using UnityEngine;
 using System.Collections;
 
 public class RotateArea : MonoBehaviour {
 
 public bool isInRotateZone = false;
 public GUIText referencetotext;
 
 
     // Use this for initialization
     void OnTriggerEnter ()
     {
         isInRotateZone = true;
         referencetotext.text ="Press Z or X to change lane";
     }
     
 
     void OnTriggerExit()
     {
         isInRotateZone = false;
         referencetotext.text ="";
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }




Any help is appreciated

Comment
Add comment · Show 4
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 suribe · Mar 09, 2014 at 11:22 PM 0
Share

What error are you getting exactly? I would guess that it's not finding any object tagged 'rotateZone' and you get a null pointer exception.

avatar image StigC · Mar 10, 2014 at 06:18 PM 0
Share

It says: "Assets/Scripts/Camera/CameraRotator.cs(18,82): error CS1061: Type UnityEngine.GameObject[]' does not contain a definition for GetComponent' and no extension method GetComponent' of type UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)"

avatar image robertbu · Mar 10, 2014 at 06:36 PM 0
Share

@StigC - FindGameObjectsWithTag() returns an array of colliders. You have to use a loop to go through and get the component on each one. The compiler is complaining because you tried to call GetComponent() on the whole array rather than an element in the array.

avatar image StigC · Mar 10, 2014 at 08:37 PM 0
Share

@robertbu - Can you help me with it or point me to an example? I've found this and this one, but aren't sure how to utilize it.

2 Replies

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

Answer by robertbu · Mar 11, 2014 at 12:29 AM

Let me write about your problem in general first. The way you are approaching this problem is down toward the bottom of the list of my choices. So if you can define the zone as a distance from a point, my first choice would be to have both RotateArea and CameraRotator use distance from a point to do its calculations.

Second choice would be to put OnTriggerEnter() and OnTriggerExit() on the player. This could could check for the kind of object (tag), and then set a flag. Your camera script could then read the information from the player.

My third choice would be to use Events and Delegates and have the RotateAreas communicate to the CameraRotator script. Here is a link for more info on Events and Delegates:

http://answers.unity3d.com/questions/600416/how-do-delegates-and-events-work.html

So here is a specific answer to your question:

At the top of the file:

 RotateArea[] ras;

In Start():

 GameObject[] gos = GameObject.FindGameObjectsWithTag("rotateZone");
 ras = new RotateArea[gos.Length];
 for(int i = 0; i < gos.Length; i++) {
     ras[i] = gos[i].GetComponent<RotateArea>();
 }

At the end of Start() you will have an array of references to all of your RotateAreas. So in the body of your script you can add this function:

 bool CanRotate() {
     foreach (RotateArea ra in ras) {
         if (ra.isInRotateZone)
             return true;
     }
     return false;
 }
 

Just call this function when you want to determine if you should be able to rotate or not.

Comment
Add comment · Show 3 · 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 StigC · Mar 16, 2014 at 04:45 PM 0
Share

I've tried and tried ins$$anonymous$$d of asking instantly. But I can't get this to work...

Is this javascript? Where should I put the bool CanRotate()... ?

At the end of Start() you will have an array of references to all of your RotateAreas.

How? I really have no clue how.

Thanks.

avatar image robertbu · Mar 16, 2014 at 06:25 PM 0
Share

This is C#. Here is my suggestion integrated into the code that you posted:

 using UnityEngine;
 using System.Collections;
 
 public class CameraRotator : $$anonymous$$onoBehaviour 
 {
     public float rotateTime = 1.0f;
     
     private bool _isTweening = false;
     private CrunchPlatformColliders _cruncher;
     private DisablePlayer _disablePlayer;
 
     RotateArea[] ras;
     
     void Start () 
     {
         _cruncher = GetComponentInChildren<CrunchPlatformColliders>();
         GameObject player = GameObject.FindGameObjectWithTag("Player");
         _disablePlayer = player.GetComponentInChildren<DisablePlayer>();
     
         GameObject[] gos = GameObject.FindGameObjectsWithTag("rotateZone");
         ras = new RotateArea[gos.Length];
         for(int i = 0; i < gos.Length; i++) {
             ras[i] = gos[i].GetComponent<RotateArea>();
         }
     }
     
     
     
     void Update(){
         if(CanRotate())
         {
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Z))
             {
                 rotateTween(90);
             }
             
             if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X))
             {
                 rotateTween(-90);
             }
         }
     }
     
     private void rotateTween(float amount) 
     {
         if (_isTweening == false) 
         {
             _isTweening = true;
             //       _cruncher.setPlayerPos();
             _disablePlayer.disable();
             Vector3 rot = new Vector3(0,amount, 0);
             iTween.RotateAdd(gameObject, iTween.Hash(iT.RotateAdd.time, rotateTime, iT.RotateAdd.amount, rot, iT.RotateAdd.easetype, iTween.EaseType.easeInOutSine, iT.RotateAdd.oncomplete, "onColorTweenComplete"));
         }
     }
     
     private void onColorTweenComplete()
     {
         _isTweening = false;
         _disablePlayer.enable();
         _cruncher = GetComponentInChildren<CrunchPlatformColliders>();
         //_cruncher.crunchCollidersToPlayer();
     }
 
     bool CanRotate() {
         foreach (RotateArea ra in ras) {
             if (ra.isInRotateZone)
                 return true;
         }
         return false;
     }
 }
avatar image StigC · Mar 16, 2014 at 06:35 PM 0
Share

Thank you so very much! Really appreciate it. It helps a lot seeing how it's supposed to be set up. I really didn't understand how to use bool CanRotate, or what to make of the void Update. Thanks alot!

avatar image
0

Answer by TropicalTrevor · Mar 16, 2014 at 04:59 PM

this is C#. sadly I don't really know UnityScript so you may need someone to translate this to js ;)

In your RotateArea script you can declare a static variable such as

 public static List<RotateArea> areas = new List<RotateArea>();

then in RotateArea.Awake() you will get this:

 if(!RotateArea.areas.Contains(this));
     RotateArea.areas.Add(this);

then in any other script you can access RotateArea.areas and know about all the currently existing rotate areas

to do this cleanly you will need to have an OnDestroy function in RotateArea that handles the deletion of the object:

 void OnDestroy(){ RotateArea.areas.Remove(this); }

I like this method because it is contained within the RotateArea script and allows more generic access of these objects without ever resorting to Find functions.

Also Events can be a problem because when you use event listeners it becomes very difficult to debug, you can't really see what objects are listening to what events of what other objects and this way your code becomes fairly unclear.

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

22 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

Related Questions

C# GetComponent Issue 2 Answers

C# GetComponent / change values throught other script 3 Answers

how to make one script change a variable in another scipt 2 Answers

Accessing Enumeration from another script issue 1 Answer

Getcomponent error? 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