Hi All,
What is the difference between AddComponent and GetComponent, and can anyone point me to a good example based tutorial on how to use them in C#?
I have two C# scripts, which are supposed to be getting and setting variables from each other. One way (script A → script B), it works fine, but if I try to do it both ways (script A ↔ script B), it crashes Unity with no log. I assume it is because of my incomplete understanding of how it works, so am looking for a good tutorial on it.
Cheers,
EN
AddComponent adds a new component; GetComponent returns an already existing component.
–Eric
Thanks Eric. To clarify a little further, does adding a ‘new’ component mean ‘new to the script in which the AddComponent line is written’? And does an ‘already existing component’ mean ‘already existing in the script in which the GetComponent line is written’? Or, am I thinking about this wrong?
EN
No, it means new to the actual object in the scene. Say an object has no AudioSource component…after using AddComponent with an AudioSource, now it does, and can play audio.
Nope, already existing on the actual object in the scene.
–Eric
Thanks Eric, that makes sense.
Hi All,
I still have a problem with AddComponent. As above I am trying to communicate both ways between scriptA and scriptB. Both scripts add each other as components, and it all compiles fine. However, scriptA predictably crashes the Unity Editor when testing the game. If I delete the AddComponent from scriptA, so that only scriptB is adding scriptA as a component, it all works perfectly. scriptA is a very long and complex script, so I have done a simple test with two simple scripts to make sure my logic and execution are okay - the simple test works fine. It is a bad crash that leaves no log.
Does anyone know of any gotchas around AddComponent that might be causing this horrible crash, even though the scripts compile with no errors?
EN
I don’t think that’s possible…if Script A is adding Script B, then Script A already exists and doesn’t need to be added. Script A can assign a reference to itself in Script B when adding Script B anyway.
–Eric
This is what I’m trying to do, in simplified form. It compiles fine, and as far as I can see it works fine:
scriptA:
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class ScriptA : MonoBehaviour {
public int varA = 1;
public GameObject newGameObject;
public ScriptB addB;
// Use this for initialization
void Start () {
//print("varA: "+varA);
newGameObject = new GameObject();
addB = newGameObject.AddComponent<ScriptB>();
}
// Update is called once per frame
void Update () {
print("varA: "+varA);
if(Input.GetKeyDown("b")){
addB.varB += 1;
}
}
}
scriptB:
using UnityEngine;
using System.Collections;
public class ScriptB : MonoBehaviour {
public int varB = 0;
public GameObject newGameObject;
public ScriptA addA;
// Use this for initialization
void Start () {
//print("varB: "+varB);
newGameObject = new GameObject();
addA = newGameObject.AddComponent<ScriptA>();
}
// Update is called once per frame
void Update () {
print("varB: "+varB);
if(Input.GetKeyDown("a")){
addA.varA += 1;
}
}
}
However, when I try this in my actual project, where scriptA is very long and complex, the Unity Editor will crash when testing the game if, and only if, I have scriptA add scriptB as a component. In other words, if i remove the line “addB = newGameObject.AddComponent();” it will run fine. If I put the line back in, it will crash the Unity Editor immediately, with no log.
Clearly, I am doing something wrong (I am the Eternal Noob after all
which assumedly involves me misusing the AddComponent. If someone could shed some light, I’d be most appreciative!
EN