Forum rules - please read before posting.

Assigning items from a prefab

Hey folks,

I'm brand new to AC, and while I've been making games for many years, I'm an artist so scripting is pretty new to me. As such, I'm getting stuck with a fairly newbie question: how do I assign a game object to an in-scene script if that game object is within a prefab?

For example, I'm doing the tutorial about examining objects up close.

https://adventurecreator.org/tutorials/examining-objects-close

(Btw, I'm on Unity 2021.1.6f1. And I've downloaded the more updated script from the wiki that compiles nicely...I guess because of get.component problems).

Now I have a marker in my first person character prefab called "HeldItemMarker," and if I drag it into the scene I can assign it to the Return Pick Up script no problem.

The problem arises when I delete the character prefab from the scene. I don't want my character to live in the scene always. I want to be able to spawn when the game starts. But if I do so, the Return Pick Up component complains about the marker going missing.

So, how do I correctly assign markers (and other game objects) to scripts, action lists, etc without them getting upset?

Thank you!

Comments

  • Welcome to the community, @sturgical.

    First off - my apologies for the delayed reply. I'm afraid I missed your post yesterday.

    In the case of AC Actions and Editor fields, AC relies on its own "Constant ID" system to link scene and prefab objects together.

    For example, if you were to use an Object: Teleport Action in the scene, and assign a prefab in its Object to teleport field, you should find that AC generates a Constant ID component on the object, and displays its ID value underneath the field. This ID value will be used to connect the Action to the scene instance of the prefab at runtime.

    Some tutorials on this topic can be found here.

    In the case of the linked tutorial's ReturnPickUp script, however, the Constant ID system isn't incorporated. To link the Marker field to the scene-instance at runtime, you'd have to modify the script. There's a few approaches you can take, but the easiest is to instead refer to the "Marker When Held" GameObject by name.

    Here's an alternative to the script (ReturnPickUp_Name.cs) that lets you enter in the name of the Marker GameObject instead:

    https://pasteall.org/u9CV

  • edited May 2022

    Thanks so much! It works (of course). Unity complained a little about the way get component was being used, but I stole a fix from the old version of the script and viola. Maybe it's a 2021 LTS thing?

    I tried pasting in the code it prefered, but I guess it reads it as markdown and won't show the whole thing. Obviously lots to learn here--AC-wise and forum-wise.

    Anyway, AC has been a lot off fun so far. Excited to dig further into it and come up with more questions.

  • Regarding markdown: you just need to add a tab-intend to make code appear correct in posts.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.