The question is answered, right answer was accepted
(Solved) Enabling a script from another script
I'm making a game with two controllable characters. The characters are both in the same area and when I press a button, I want the player to take control of the other character and leave the other where it was left. I have both characters as separate objects with their own controller script but I have trouble getting them work as I want to.
What I've been trying to do is has CharacterController1 enabled at the start of the game and when I press Q, the CC1 disables and CC2 enables. My CC1 to CC2 switch looks like this:
public CharacterController1 ascript;
public CharacterController2 bscript;
void Start () {
ascript = GetComponent<CharacterController1> ();
bscript = GetComponent<CharacterController2> ();
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Q))
{
ascript.enabled = !ascript.enabled;
bscript.enabled = bscript.enabled;
}
}
The CC2 has a similar script but the Update part is the opposite way. The problem I run to is that when I press play both of my CCs are enabled and so I control both character. When I press Q CC1 disables like it should but I can't get the CC2 to disable when I only want to control Character1.
I've tried adding lines like:
if (ascript.enabled) { bscript.enabled = !bscript.enabled; }
or
void Start() { bscript.enabled = !bscript.enabled; }
To the CC2 script but I can't get it to work.
Also I keep getting this Error message: NullReferenceException: Object reference not set to an instance of an object.
I have dragged the scripts to the inspector spaces made by the public CC1 and public CC2 and didn't have a problem there but when I press play and looks at Character1 for example the CC2 has disappeared from there. I have never done coding before and I have been googling and trying different things for hours but can't figure out what I'm doing wrong.
you need to include the complete error message and the script it references (unless it's really long) so we can see exactly which line produces the error.
for reference, GetComponent
will get the component from the current GameObject
. if you want to get it from a different object, you'll need to be more explicit. something like:
someOtherComponentReference = someOtherObject.GetComponent<SomeOtherComponent>();
if you're new to coding, i'd suggest going through some of the unity tutorials until you get the hang of things.
Didn't get that to work but I did some more googling and tried different things and managed to get rid of the error. $$anonymous$$y scripts look like this now:
public class CC1toCC2 : $$anonymous$$onoBehaviour {
public CharacterController1 ascript;
public CharacterCOntroller2 bscript;
void Start () {
ascript = GetComponent<CharacterController1> ();
CharacterController2 bscript = GetComponent<CharacterController2> ();
}
void Update ()
{
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Q))
{
ascript.enabled = !ascript.enabled;
bscript.enabled = bscript.enabled;
}
if (ascript.enabled)
{
bscript.enabled = !bscript.enabled;
}
}
}
and
public class CC2toCC1 : $$anonymous$$onoBehaviour {
public CharacterController1 ascript;
public CharacterCOntroller2 bscript;
void Start () {
CharacterController1 ascipt = GetComponent<CharacterController1> ();
bscript = GetComponent<CharacterController2> ();
}
void Update ()
{
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Q))
{
ascript.enabled = ascript.enabled;
bscript.enabled = !bscript.enabled;
}
}
}
This is how close I've gotten to get this to work: CC1 works perfectly and it disables and enabled fine like it's supposed to. But CC2 only works about 30% of the time. When CC1 is enabled and working, the tick on CC2 flashed on the inspector (I can't control the C2 though so that's not really a problem?) but when I try to switch to C2 the flashing usually stops and the script stays unticked. 30% of the time it goes ticked like it should. I need to switch back to C1 a couple of times to get it to work.
I've gone through all the scripting tutorials for beginners on Unity's website and I feel like I understand how things work but as soon as I start actually doing something nothing seems to work and I don't understand why. I've tried to find similar situations here and try thing that worked for those but I just can't get things to work. :/
Answer by Halleester · Sep 30, 2016 at 02:07 PM
Before I answer: Instead of doing script.enabled = script.enabled;
with both a and b script, try setting it equal to true or false, like this: script.enabled = true;
. It will be a lot less confusing later on. Back to the question: What you are trying to do seems a lot more convoluted than it should be, and the scripts are probably both trying to swap at the same time, creating a loop between the two functions swapping scripts on and off, creating a race for which script calls the button press last. You could simplify this to one simple script:
public CharacterController1 ascript;
public CharacterController2 bscript;
void Start () {
ascript = GetComponent<CharacterController1> ();
bscript = GetComponent<CharacterController2> ();
ascript.enabled = true;
bscript.enabled = false;
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.Q))
{
if(ascript.enabled == true)
{
ascript.enabled = false;
bscript.enabled = true;
}
else
{
ascript.enabled = true;
bscript.enabled = false;
}
}
}
This should work, and works inside one script. I didn't test it, so let me know if it has an error.
Thank you! That works perfectly.
I tried using true and false at one point but I was unable to work them so I could switch between more than once. Apparently I just did something wrong with that too.
But thank you, this was a huge help.
Any time! If you mark my question as correct, then it will automatically put it into the solved category so other people won't try to answer your question as well, and works better than putting"solved" in the title. :D
Answer by Dak28 · Jan 12, 2017 at 05:03 PM
It dosen't work for me it give me this error: NullReferenceException: Object reference not set to an instance of an object StartGame.Update () (at Assets/Scripts/StartGame.cs:53)
but i have the script Catapult
Solved will work
public GameObject otherobj;//your other object
public string scr;// your secound script name
void Start () {
(otherobj. GetComponent(scr) as $$anonymous$$onoBehaviour).enabled = false;
}