Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AryaIsMe · Apr 13, 2021 at 05:55 PM · unityeditorinspector

Why can't I attach my Player to a transform in my script?

I am trying to make a script where the zombie looks for the player and if the player enters the boundaries it will start chasing it. My problem is that I can't attach my player to the script in the inspector and I am unsure why. I don't have a problem with the code(at least I don't think so) only with it not attaching. The player is a prefab from the unity Standard assets. Edit: The script is attached to a prefab in my assets. The player in in the hierarchy.

Just in case here is my code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ZK_attack : MonoBehaviour
 {    
     public Transform Player;
     public float MoveSpeed = 4.5f;
     public float MaxDist = 4.0f;
     public float MinDist = 1.5f;
     private Coroutine animat = null;
     private Animator anim;
 
 
     void Update()
     {
         
 
         transform.LookAt(Player);
 
         if (Vector3.Distance(transform.position, Player.position) >= MinDist)
         {
             transform.position += transform.forward * MoveSpeed * Time.deltaTime;
             anim.SetBool("inRadius", true);
 
             if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
             {
                 anim.SetBool("AttackingPlayer", true);
 
 
             }
 
         }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HellsHand · Apr 13, 2021 at 06:09 PM 0
Share

Code looks fine. Rather odd it wouldn't let you as anything with a Transform should be able to be dropped into Player which is just about everything that isn't UI. Just to be sure, you are dragging the object onto a script that is currently on another object correct? As opposed to trying to drop it into the script directly in Assets.

avatar image AryaIsMe HellsHand · Apr 13, 2021 at 06:21 PM 0
Share

The script is attached to a prefab in my assets. The player in in the hierarchy.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by HellsHand · Apr 13, 2021 at 06:34 PM

Ok, the problem is that you can only drag objects into scripts that are in your scene already, unless they are prefabs themselves in the assests folder, even though it is on an object. To accomplish what you need, in your Start() add Player = GameObject.Find("...name of object you wish to set as Player").transform;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image krupps · Nov 12, 2021 at 03:06 PM 0
Share

Don't use GameObject.Find. It's slow

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

124 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Customize how class attributes are shown in the Inspector? 0 Answers

Unity editor - sort items in any list visible in inspector 2 Answers

Serialize Custom Abstract Class (No-MonoBehaviour / No-ScriptableObject) 1 Answer

UnityEvent Inspector bug at Unity 2020.3 0 Answers

Make a custom inspector that shows a group of variables in form of list 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges