i Need Some Help Please

I am trying to make a car spawn with the press of a button on the keyboard (F) and then destroy it when i am at my destination and out of the car if that makes any sense…
Here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class KeyTriggerTest : MonoBehaviour
{
   
    public GameObject Model_Misc_Buggy1;
   
   
    // Start is called before the first frame update
    void Start()
    {
        if(Input.GetKeyDown(KeyCode.F));
       
        GameObject = (Model_Buggy1);
       
        if (Input.GetKeyDown(KeyCode.F));
       
        Destroy(Model_Misc_Buggy1);
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

Any help is GREATLY Aprreciated! :wink:

Well, you are checking for a key press in the start method, which only happens 1 time. Move that code to the update method so it checks once per frm.

Ok will give it a go… thank you!

Well, I get these two errors after moving to the update method:

Assets\KeyTriggerTest.cs(23,6): error CS0118: ‘GameObject’ is a type but is used like a variable

Assets\KeyTriggerTest.cs(23,20): error CS0103: The name ‘Model_Buggy1’ does not exist in the current context

I would suggest you brush up on some c#. Try this: Unity C# Survival Guide - Unity Learn

These lines are the problem:

if(Input.GetKeyDown(KeyCode.F));

GameObject = (Model_Buggy1);

if (Input.GetKeyDown(KeyCode.F));

Destroy(Model_Misc_Buggy1);

You are ending your if conditional statements with “;” when you shouldn’t. You haven’t either indented your code that should be part of if code block.

Your ifs should look something like this:

if (condition == true)
{
    // do something
}

Or like this, for single line blocks (note, some recommend this style should be avoided):

if (condition == true)
    // do something

This part doesn’t make sense at all:

GameObject = (Model_Buggy1);

GameObject is type, but to define a variable of a certain type, you should use something like this: GameObject myVariableName;

Also, the part on the right side of “=” doesn’t make sense either. I guess here you would want to instantiate your object and put it to your variable? See the docs for how to instantiate an object:

Second if has the same issue as the first one. And in addition to that, the code your if should execute doesn’t make sense either:

 Destroy(Model_Misc_Buggy1);

That object I guess is your prefab, which you would have instantiated when you pressed F. And you wouldn’t want to destroy your prefab, but your instantiated clone. So you should store the first if object in a private field for example, and then here destroy it:

// Your prefab to instantiate
public GameObject buggyPrefab;
// store instantiated object here
private GameObject buggyClone;

// In your if:
Destroy(buggyClone);
1 Like

This! Definitely work through some complete low-level tutorials because game software engineering is about a lot more than just typing some characters you find on a website or video. It has to be 100% perfect in all ways before it works: properly capitalized, properly spelled, and ESPECIALLY properly punctuated.

Thanks for the information!
=)

I’m trying to…

Hence the help…

I need just a little help trying to get a new script working from this same method…

I still want the same method (As in generating a cloned prefab with keycode “F” down and then destroyed by “Q”…)

I have a big world i made, and don’t want to keep running to the same buggy over and over again so i just want to instantiate it on the fly next to the player…

Need Help Doing That! :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class KetTriggerTest : MonoBehaviour


{
    public GameObject Buggy;
    // store instantiated object here
    private GameObject buggyClone;
   

    // Update is called once per frame
    void Update()
    {
       
        if(Input.GetKeyDown(KeyCode.Q));
       
        {
            Instantiate(buggyClone);
           
        }
       
        if(Input.GetKeyDown(KeyCode.G));
       
        {
            //CreatePrefab();
           
            Destroy(buggyClone);
           
           
        }
    }
       
    public void CreatePrefab()
    {
        float insX = Buggy.transform.position.x;
        float insY = Buggy.transform.position.y;
       
       
    }
}

Keep your brain straight about the prefab versus the clone.

For instance, this line clones the clone (which is likely null or destroyed):

Instead, name your Buggy prefab properly:

public GameObject BuggyPrefab;

Now when you make the thing, do this:

buggyClone = Instantiate(BuggyPrefab);

You have now confidently made a clone of the prefab, and kept a reference to it.

NOW… where do you want it?

Let’s say for argument’s sake that your player is a GameObject called Player and you want it to spawn 5 units in front of him, whichever way he is facing.

That would be code placed BEFORE the above Instantiate:

float DistanceAheadOfMe = 5.0f;

Vector3 PlayerPosition = Player.transform.position;

Vector3 BuggySpawnPosition = PlayerPosition + Player.transform.forward * DistanceAheadOfMe;

That code will compute where you want the buggy. Now… you gotta make it there!

REPLACE the earlier Instantiate call with this:

buggyClone = Instantiate(BuggyPrefab, BuggySpawnPosition. Quaternion.identity);

Read the above over, and sorta digest in your brain how it all kinda chunks together, one step at a time. That’s pretty much how everything is done. :slight_smile:

1 Like

Oh man you are Awesome!!!

THANK YOU! :smile:

1 Like

I am trying to figure out why i get these errors though but don’t understand (yet)

Assets\KetTriggerTest.cs(42,32): error CS0120: An object reference is required for the non-static field, method, or property 'Component.transform'

Assets\KetTriggerTest.cs(44,53): error CS0120: An object reference is required for the non-static field, method, or property 'Component.transform'

Perhaps because you capitalized transform? Doing so implies you want to access static class-level stuff in Transform, whereas the transform shortcut in a MonoBehavior or GameObject gives you instance-level stuff, which is the only thing I typed above.

In programming, 100% of spelling, capitalization, punctuation and spacing (or lack thereof) has to be perfect.

If that’s not it, post the line(s) of code. ALSO:

The important parts of an error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

I’m starting to understand now but here is the code:

   using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
 
    public class KetTriggerTest : MonoBehaviour
 
 
    {
        public GameObject Buggy;
        // store instantiated object here
        private GameObject buggyClone;
   
 
        // Update is called once per frame
        void Update()
        {
       
            if(Input.GetKeyDown(KeyCode.Q));
       
            {
                buggyClone = Instantiate(Buggy);
           
            }
       
            if(Input.GetKeyDown(KeyCode.G));
       
            {
                //CreatePrefab();
           
                Destroy(buggyClone);
           
           
            }
        }
       
        public void CreatePrefab()
        {
        
            float DistanceAheadOfMe = 5.0f;
 
            Vector3 PlayerPosition = FPSController.transform.position;
 
            Vector3 BuggySpawnPosition = PlayerPosition + FPSController.transform.forward * DistanceAheadOfMe;
        
            buggyClone = Instantiate(Buggy);

       
       
        }
    }

I also really appreciate the help too. :slight_smile:

This is likely the type of your player, not your actual player instance.

Make a public field of this type, drag the player in.

But again, you’re making this harder than it has to be by not pointing out the line above that has the error. :slight_smile:

1 Like

Yeah i know i am lol thank you i will try it…

I’m sorry to get you mad or upset with me but what am i doing wrong???

But nothing happens when the designated keys are pressed?

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using DarkTreeFPS;
 
    public class KetTriggerTest : MonoBehaviour
 
 
    {
        //public GameObject Buggy;
        // store instantiated object here
        private GameObject buggyClone;
   
        public GameObject BuggyPrefab;

   
        public GameObject FPSController;
  
        public GameObject BuggySpawnPosition;
 
        // Update is called once per frame
        void Update()
        {
      
            if(Input.GetKeyDown(KeyCode.Q));
      
            {
           
                BuggyPrefab = Instantiate(buggyClone);
           
                //buggyClone = Instantiate(buggyClone, BuggySpawnPosition. Quaternion.identity);
          
            }
      
            if(Input.GetKeyDown(KeyCode.E));
      
            {
                //CreatePrefab();
          
                Destroy(buggyClone);
           
                float DistanceAheadOfMe = 5.0f;
 
                Vector3 FPSControllerPosition = FPSController.transform.position;
 
                Vector3 BuggySpawnPosition = FPSControllerPosition + FPSController.transform.forward * DistanceAheadOfMe;

          
          
            }
        }
    }

Here is the error:

ArgumentException: The Object you want to instantiate is null.
UnityEngine.Object.Instantiate[T] (T original) (at :0)
KetTriggerTest.Update () (at Assets/KetTriggerTest.cs:29)

And just to let you know i AM TRYING to learn too… :slight_smile:
(But i tend to overthink things a little…)

Did you re-drag the prefab into the newly-renamed BuggyPrefab field?

ALSO: you do NOT want semicolons at the end of your if lines. That effectively closes the if statement and prevents it from properly controlling the next block of code, making that block run unconditionally.

ahhh i see hence the spawning of a million of them lol