Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed May 25, 2016 at 07:53 PM by mnmwert for the following reason:

The question is answered, right answer was accepted

avatar image
-1
Question by mnmwert · May 12, 2016 at 04:29 PM · transformbugdestroyvariablebullet

Bug: cannot assign public Transform variable. Unity 2D 5.2

HI,

I have a script that destroys a bullet when it collides with something. but i want it to reduce the health of my player. so I declared

public Transform player;

i come back to the inspector and try to assign the variable but it Does Not Assign. the problem is that it will not let me choose from my list of people in the Hierarchy! What must I do??

Here is my code:

using UnityEngine; using System.Collections;

public class EnemyShotDestroy : MonoBehaviour { public Transform player;

 private void OnCollisionEnter(Collision collision)
 {
     if (collision.gameObject.tag == "Player")
     {
         Destroy(gameObject);
         player.GetComponent<mover>().health  -= 1;
     }
     if (collision.gameObject.tag == "Boundary")
     {
         Destroy(gameObject);
     }
     if (collision.gameObject.tag == "Bullet")
     {
         Destroy(collision.gameObject);
         Destroy(gameObject);
     }
 }
 

}

I attached this into the prefab of my EnemyBullet in Assets and when i try to assign the var. it doesn't work. When i bring it out into the World it does let me assign but its clones dont work... Heres by EnemyBehavior Script:

using UnityEngine; using System.Collections;

public class groundEnemyBehavior : MonoBehaviour {

 public float moveSpeed;
 private float move = 1;
 public float nextFire;
 public float fireSpeed;
 public Transform BulletSpawn;
 public float fireRate;
 public GameObject shot;
 private GameObject cloneShot;
  
 void Start ()
 {
  
 }
 
 

 void FixedUpdate ()
 {
     if (move == 1)
     {
         GetComponent<Rigidbody>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody>().velocity.y);
     }
     if (Time.time > nextFire)
     {
         nextFire = Time.time + fireRate; 
         cloneShot = (GameObject)Instantiate(shot, BulletSpawn.position, BulletSpawn.rotation);
         cloneShot.GetComponent<Rigidbody>().velocity = new Vector3(-fireSpeed, 0);
      }
    
 }
 

}

Thanks for all who help I am really annoyed at this.

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by Tyche10 · May 12, 2016 at 05:11 PM

You cannot drag a gameobject from your scene hierarchy into a script attached to a prefab because the gameobject is part of the scene and is not always present like assets or resources are. You will need to look for it during runtime with a function like GameObject.FindObjectWithTag() or something similar.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

52 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Possible bug? Unity forces transform Y scale to 0 when reordering in hierarchy, disappears if you try to fix 0 Answers

Applying a Prefab to a Variable 1 Answer

Need help with a "roll" script (C#) 1 Answer

Why does my Bullet prefab go missing from my gun script when destroyed? 1 Answer


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