- Home /
Not answered as unable to fully explain. Decided to approach another way.
Using a String to populate GetComponent
Hi,
I need to dynamically create the 'Path' to a boolean value in an external script, but I get the following error:
MissingFieldException: Field 'SpawnAttackersAndDefenders.spawnModeIdentity' not found.
Here is the code so far:
var spawnAttackersAndDefendersScript : SpawnAttackersAndDefenders;
//The script that spawns the object this is attached to advises the name of the boolean to be changed
var spawnModeIdentity : String;
function Start(){
spawnAttackersAndDefendersScript = GameObject.Find("Start").GetComponent(SpawnAttackersAndDefenders);
spawnAttackersAndDefendersScript.spawnModeIdentity = true; //Script fails here
}
function OnTriggerEnter (other : Collider) {
spawnAttackersAndDefendersScript.spawnModeIdentity = false; //Script fails here
}
How do it correctly 'insert' the name of the boolean variable I am trying to target?
[UPDATE] To clarify, the var 'spawnModeIdentity' is NOT the real name of the boolean I am trying to target. It is simply a way of dynamically changing which boolean to target.
For example:
Script A has 3 Booleans:
var test1 = true;
var test2 = true;
var test3 = true;
Script B is designed to dynamically target only one of the Booleans in script A. Script A tells which Boolean Script B is to target using the following code:
scriptB.spawnModeIdentity = "test1"
This part is working fine.
From there I am trying to then correctly pass the new Boolean back to the relevant Boolean in Script A.
Thanks
Paul
Answer by fafase · Dec 01, 2012 at 04:06 PM
You are trying to pass a boolean to a string. I think you want to do:
spawnAttackersAndDefendersScript.spawnModeIdentity = "true";
But why not using a boolean from the start?
Hi fafase,
Thanks for responding. However that is not what I am trying to do. I have updated my question, to try and explain better. It's sometimes feels harder to explain than the actual problem itself.
I have worked around the problem in a different way, and will be closing this question. Thank you for assisting fafase.