- Home /
RequireComponent with custom public vars?
Hi, is there a way to assign variables to ScriptA, which is required by ScriptZ, so that these variables already show up in the editor? I want to accomplish some sort of auto-setup: A GameObject needs script A,B,C and I just want to assign script Z which assigns script A, B, C already with the right public variables. I can only imagine to do this in the Awake function but then false variables will be shown while still in the editor...
Example Code:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(ScriptA))]
public class ScriptB : MonoBehaviour {
public int a = 1;
public int b = 2;
void Awake() {
ScriptA scriptA = GetComponent<ScriptA>();
scriptA.a = a; // when game is inactive maybe ScriptA shows 42 instead of 1
}
void Update() {
do_something...
}
}
Answer by perchik · Feb 25, 2014 at 06:18 PM
Trying moving your code to Start() not awake
this would not change anything (already tested), I want to assign scriptB (with let's say an public int x = 3) per drag and drop in the editor and then already have this public int x from scriptB assigned to scriptA to a public int. everything before the game runs
Do you perhaps want an editor script to add the correct scripts and assign the variables? (seems like the right way to go about it)