Y
Y
Yura Berezovsky2016-10-06 21:22:39
Programming
Yura Berezovsky, 2016-10-06 21:22:39

How to activate scripts of other objects in a script?

The script should work like this, when a certain key is pressed, the Light (Sphere) activation script is triggered, and also when another key is pressed, another Light (Sphere (1)) is activated (the script must be independent of these objects)
96f66de9556d4d7ea8a647f9c9998dd8.pngf8a3314c044d4940b16494aca00ef959.pngcba19dc45e814283a400ef5f36637163.png
Main script code

using UnityEngine;
using System.Collections;

public class TimeLightsScript : MonoBehaviour {
  
  public GameObject[] light;
  private 

  


  void Start () {
    light = GameObject.FindGameObjectsWithTag ("MainLight");
  }
  

  void Update () {
    
  }
}

child object code
using UnityEngine;
using System.Collections;

public class TimeLightsScript : MonoBehaviour {
  
  public GameObject[] light;
  private 

  


  void Start () {
    light = GameObject.FindGameObjectsWithTag ("MainLight");
  }
  

  void Update () {
    
  }
}

PS I don't want to write separate scripts for each object

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GreatRash, 2016-10-07
@Jurajkeee

The script is also a component, therefore:

public class SomeClass: MonoBehaviour {
  private otherSctipt: OtherScript;

  public void Start() {
    otherScript = GetComponent<OtherScript>();
    otherScript.someOtherMethod();
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question