简体   繁体   中英

Unity prefab as an object in inspector asking for attaching object

I am initiating prefabs as objects and then trying to do something using these objects that have those prefabs. However, in the inspector, its asking to attach objects that are actually already attached via code.

Code

public float hitspeed=2f;
public GameObject target, mytarget;
public GameObject arrow,myarrow;

//Use this for initialization
void Start (){
    myarrow = Instantiate(arrow, new Vector3(0.02f, -3.98f, 0), transform.rotation);
    mytarget = Instantiate(target, new Vector3(-0.06435323f, 2.325303f, 0f), transform.rotation);
}

Inspector

检查员

Error

[06:34:34] UnassignedReferenceException: The variable arrow of knife has not been assigned. You probably need to assign the arrow variable of the knife script in the inspector.

[06:34:39] UnassignedReferenceException: The variable myarrow of knife has not been assigned. You probably need to assign the myarrow variable of the knife script in the inspector.

With your object selected so you can see the inspector shown in the image

Go to your Project window, find your prefab.

Drag it to the the inspector where it says "none."

You can also click the little target/circle next to "none" and choose from a list.

It is true that you seem to have assigned myarrow and mytarget , however, you have instantiated them using target and arrow . You have set target using the gui, but not arrow . You must also set arrow .

In general, and to quote from another answer :

[ arrow and target have] to be a prefab that you've already created in the Editor and dragged/dropped onto the Inspector window for this object.

Ensure you:

  1. Created a prefab

  2. Selected this object in the Editor, so the Inspector appears

  3. Drag/dropped that prefab onto the "arrow"/"target" field in the Inspector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM