I want to check if variable is missing(not null),Can't check if object has missing variable
As shown on pictures I wan't to destroy an object if it has a missing transform variable on script.
currentLeadTransform
is a Transform
variable. I wan't to destroy gameObject
if it is missing(not null).
**if (gameObject.GetComponent().currentLeadTransform == null) Destroy(gameObject);**
I have tried code above but it didn't work.
Thanks to everyone who want to help. Much love.
Do not post code as images as it makes harder for people to respond to you. There is a option for code formatting here so you can copy&paste it just fine.
Overall, your code works fine. The thing is that you don't understand what does what yet. Work on that mate.
gameObject.GetComponent<ClassYouAreWritingThisCodeIn>()
means the same as this
("this class instance"). So, your code does this:
void Update ()
{
if( current$$anonymous$$Transform==null ) return;
transform.position = some_new_value;
if( current$$anonymous$$Transform==null ) Destroy( gameObject );
}
The second if
statement will never pass as true
. Think this through, take your time.
Oh, and one more thing: null
means "missing".
To test for "not missing": if( variable != null )
Your answer
Follow this Question
Related Questions
Referencing variable from another script on another object 3 Answers
Not sure how to make my classes interact in the way I intend them to 1 Answer
Make object move back and forth 2 Answers
Vector3.Lerp not working properly, making the player bounce around 2 Answers
Vector3.MoveTowards moving transform 0 Answers