- Home /
C# assigning components to tag
Don't know how to make that work :
using UnityEngine;
using System.Collections;
public class AsignScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
#if UNITY_EDITOR
// Update is called once per frame
void ExecuteInEditMode () {
GameObject[] bloc = GameObject.FindGameObjectsWithTag ("Bloc");
if (Application.isEditor) {
foreach (GameObject blc in bloc) {
if ( !blc.GetComponent<MouseControlScript>())
blc.AddComponent<Rigidbody2D> ();
blc.AddComponent<BoxCollider2D> ();
blc.AddComponent<MouseControlScript> ();
}
}
}
#endif
}
Any clue ?
Comment
Answer by Cence99 · Aug 07, 2016 at 10:41 PM
Not sure, but probably a syntax error? Instead of doing this
if ( !blc.GetComponent<MouseControlScript>())
blc.AddComponent<Rigidbody2D> ();
blc.AddComponent<BoxCollider2D> ();
blc.AddComponent<MouseControlScript> ();
Try this
if ( !blc.GetComponent<MouseControlScript>())
{
blc.AddComponent<Rigidbody2D> ();
blc.AddComponent<BoxCollider2D> ();
blc.AddComponent<MouseControlScript> ();
}
Your answer
Follow this Question
Related Questions
Custom [Require*] attribute 2 Answers
Multiple Cars not working 1 Answer
RequireComponent at variable level? 1 Answer
Distribute terrain in zones 3 Answers