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 /
  • Help Room /
avatar image
0
Question by mraths · Oct 07, 2015 at 09:40 PM · javascriptinstantiateprefab

Unity3d 4.6 javascript prefab with script

Hi I created a prefab named block1 and added a script on them named sheepController which does following:

 var startingHealth:int = 2;
 var currentHealth:int;
 
 var isDead:boolean;
 
 function Awake () {
     currentHealth = startingHealth;
 }
 
 public function HitSheep (sheep:GameObject) {
     
     Debug.Log("Hit Sheep startet");
     Debug.Log("Current Health: " + currentHealth);
     
     if (isDead)
     {
         Debug.Log("Sheep is dead");
         return;
     }
     
     //currentHealth = currentHealth - 1;
     currentHealth -= 1;
     Debug.Log("Current Health: " + currentHealth);
     
     if (currentHealth <= 0)
     {
         Debug.Log("Current Health <= 0");
         Destroy(sheep);
         Debug.Log("Destroy Sheep");
         isDead = true;
     }
 }

After that I instantiate the prefab (block1) in another script:

 function createBlocks()
 {       
         for(var j:int = -5; j<6; j++)
         {
             Instantiate(block1, Vector3(j*21, 20, 0), Quaternion.identity);
         }
 }

On Collision I like to subtract health of block1 by 1 each hit until it reached <= 0 .Then the block should be destroyed.

 function OnCollisionEnter(col:Collision)
 {    
     if (col.gameObject.tag == "block1")
     {
         Debug.Log("Hit Block1");
         audio.PlayOneShot(bricksound,0.2);
         
         var scontroller:sheepController;
         scontroller = GameObject.FindGameObjectWithTag("block1").GetComponent(sheepController);
         if (col.gameObject != null)
         {
             scontroller.HitSheep(col.gameObject);
 
         } else {
             Debug.Log("col.gameObject is null!");
         }
         
         if (gameController.score >= 0)
         {
             gameController.score = gameController.score + 10;
         }
     }
 }

The problem now the prefab count is not set for each prefab. It means if count is less then 0 on prefab hit it will be destroyed. I like to have each prefab has a start by 2 and subtracting by 1. Means two hits until destroy.

What I'm doing wrong? Thanks for help...

Comment
Add comment · Show 1
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 mraths · Oct 09, 2015 at 01:54 PM 0
Share

anybody can help?

1 Reply

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

Answer by Bilelmnasser · Oct 09, 2015 at 02:16 PM

ths problem is here line 9 :

 scontroller = GameObject.FindGameObjectWithTag("block1").GetComponent(sheepController);

EDIT : replace it with :

 scontroller = collision.gameObject.GetComponent<sheepController>();

GameObject.FindGameObjectWithTag will search for first block in your scene got this tag and in that case you always call HitSheep of one Block if you have many block in your scene !!

Comment
Add comment · Show 2 · 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 Landern · Oct 09, 2015 at 02:21 PM 1
Share

Best to use the generic GetComponet over the one that accepts a string or type as a parameter. In the non-generic case it will return type component, this is not what is desired based off the strongly typed variable.

  collision.gameObject.GetComponent(sheepController);  // this will return type of Component

Should be:

  collision.gameObject.GetComponent.<sheepController>(); // this will return type of sheepController

Just a suggestion

avatar image mraths · Oct 09, 2015 at 06:25 PM 1
Share

Thanks, work fine now with: col.gameObject.GetComponent(sheepController);

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

35 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

Related Questions

Create gameobject in other scene... 0 Answers

Im having an issue when Instantiate Prefab with specific rotation 2 Answers

Place prefab with mouse on to a grid based surface with a correct orientation. c# 0 Answers

Can't Instantiate an object as child of another 1 Answer

Cannot convert UnitEngine.Vector3 to UnityEngine.Transform when using .postition 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