The question is answered, right answer was accepted
How do I call the immobilize function to kill the AI jet?
I'm looking at the AeroplaneController.cs script attached to the AI Jet from the Standard Assets Package. It has a function for "immobilizing" the plane...
How would I go about calling this function? Here is the pertinent code from AeroplaneController.cs:
// Immobilize can be called from other objects, for example if this plane is hit by a weapon and should become uncontrollable
public void Immobilize()
{
m_Immobilized = true;
}
I have a "weapon" which is fired by left mouse click, and I want to call that Immobilize function when I shoot a jet...
Also, it occurs to me that if I'm using multiple AI jets, I might have a problem wherein I disable all of them whenever I kill one of them...
I'm very new to coding (trying to help my daughter get started with unity) so if anyone has any helpful suggestions or advice, that would be greatly appreciated.
We tried
void Update()
{
if(Input.GetMouseButtonDown(0) == true)
{
AeroplaneController.m_Immobilized = true;
}
}
but we get the error: Unknown identifier: AeroplaneController...
Clearly we're missing something, but we've been unable to find the solution.
Thanks in advance, to anyone willing to point us in the right direction.
Cheers!
Answer by GKuhns · Feb 16, 2017 at 04:34 AM
Thank you Igor,
I tried putting what you wrote into a script, but I get errors: "The type or namespace name 'AeroplaneController' could not be found. Are you missing 'UnityStandardAssets.Vehicles.Aeroplane' using directive?
I started watching the video, but it's difficult to jump into the middle of a tutorial series - I think I'll have to check it out from the start to understand better.
Also, I'm confused; would the "gameObject from where (I) want the code to work" be the AI Jet I'm trying to kill? It already has the AeroplaneController script attached to it... Sorry, I'm not quite understanding what I should do...
This is what I thought you meant, but it gets the aforementioned error:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JetKill : MonoBehaviour {
public AeroplaneController incarnationOfAeroController;
void Update()
{
if(Input.GetMouseButtonDown(0) == true)
{
incarnationOfAeroController.Immobilize();
}
}
}
What am I doing wrong?
Thanks again, I appreciate your willingness to help - sorry I'm still a bit confused.
Yes, this code looks fine
Also, I'm confused; would the "gameObject from where (I) want the code to work" be the AI Jet I'm trying to kill?
Could be, but it makes more sense to add it to the player or someone who tries to kill the jet. Hence, we should stick the script you've mentioned on a player, etc. We then prepare a slot, then specify which object we are trying to imobilize by dragging it into the slot (from the Hierarchy panel)
Also, check this link to resolve the error; $$anonymous$$ost of the errors can be copy-pasted into google, but sometimes require simplification. For example, in future the errors might be similiar to the errors of other users, but might have different file names. Hence, simplify the google query to amend these.
Yes! Thank you so much - this has been driving me nuts, and I am thrilled to finally have the right answer.
This info will undoubtedly help me in numerous instances in the future, so, many more thanks in advance!
:)
Answer by IgorAherne · Feb 16, 2017 at 01:59 AM
There are concept of Classes and Objects
Objects are physical instances of classes. The latter you can think of as some sort of "blueprints", a non-existing set of instructions.
with AeroplaneController.m_Immobilized = true;
you just ordered a "blueprint" to execute something. But it can't - it's not physically present anywhere, it's just a blueprint.
You instead need to find objects in the scene which are the incarnations of such a class. These objects are monobehaviors, which are attached to gameObjects, similar to any other component.
You can do it with GameObject.Find()
which a fairly expensive function to call, don't do it every Update.
Alternativelly, you can create a slot, into which you can drag-drop the object. You should declare the slot from inside of your script. In the script where you want to have access via the slot, write:
public YourBluePrintName yourVariableName;
for example AeroplaneController incarnationOfAeroController;
You can then drag-and-drop this monobehavior "blueprint" onto the gameObject from where you want the code to work. Now, drag-and drop the gameObject which has AeroplaneController script attached to it, into the slot.
Don't forget to do something with this slot, so it's not invain. You should do it from inside the script, for example
void Update()
{
if(Input.GetMouseButtonDown(0) == true)
{
incarnationOfAeroController.Immobilize();
}
}
At the current stage, this video is pretty much the best you can find (I've scrubbed to the needed time, you will enjoy this lesson like many others)
https://youtu.be/auXSM1CvRVc?t=1h48m17s
:)