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 benk0913 · Dec 05, 2011 at 08:08 PM · c#instantiateaimissinggame object

Missing (Game Object)

Hellom, people. Ive got a small problem with my code that I can not solve. I'm trying to create a new AI to my roach and ive got a small problem with destroying the blood and instantiating it after. At the beggining the roach instantiates the blood but then when the blood destroys itself the GameObject var that it's value is the bloods prefab just shows " Missing (Game Object) and then it just wont instatiate the blood.

Heres the roachs AI :

     using UnityEngine;using System.Collections;
 
 
 
 
 
 
 
 
 public class AI_roach : MonoBehaviour 
 {
   public float distenemy;
   public bool combat=false;
   public float movespeed =5;
   public float rotationSpeed= 5;
   public float cooldown=0;
   public float hittype=0;
   public bool spotted = false;
   public float health = 100;
   public GameObject bloodfab;
   public GameObject enemy;
     
   void Start()
     {
         GameObject bloodfab = GameObject.FindGameObjectWithTag("bloodt");
     }
   void Awake()
     {
             animation["walk"].speed = 1;
             animation["attack2"].speed = 0.7f;
             animation["attack1"].speed = 0.7f;
     }
     
   void Update()
     {
         GameObject bloodfab = GameObject.FindGameObjectWithTag("bloodt");
         distenemy = Vector3.Distance(enemy.transform.position,transform.position);
         
     if(combat && character_stats.health>0)
     {
     animation.Stop("walk");
     Combat();
     }            
     
     if(distenemy>3 && spotted)
     {
         combat=false;
          WalkToEnemy();
     }
     else if(distenemy<=3 && spotted)
     {
         animation.Stop("walk");
         combat=true;
     }
     else
     Rest();
     
     
     if(distenemy<10)
     {
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(enemy.transform.position - transform.position), rotationSpeed *Time.deltaTime);








          if (!Physics.Linecast (transform.position, enemy.transform.position)&& !spotted)
          {
              spotted=true;
          }
          
     }
 }
 
 void WalkToEnemy()
 {
     transform.position+=transform.forward * movespeed * Time.deltaTime;
     animation.Play("walk");
 }
 
 void Rest()
 {
     animation.Play("idle");
 }
 
 // _____________COMBAT_________________
 //_____________________________________
 //_____________________________________
 
 void Combat()
 {
     if(cooldown>0)
         cooldown-=1*Time.deltaTime;
     if(cooldown<=0)
     {
         hittype=Random.Range(0,4);
         if(hittype==3)
         ThrowHit();
         if(hittype<3)
             RegularHit();
     }
     if (!animation.isPlaying)
     animation.Play("idle");
     
     if(character_stats.health<=0)
     combat=false;
     
 }
 
 void RegularHit()
 {
     animation.Play("attack2");
     if(distenemy<3)
     {
         bloodfab = Instantiate(bloodfab, enemy.transform.position , enemy.transform.rotation) as GameObject;
         character_stats.health-=Random.Range(3,15);
     }
     cooldown=1.5f;
     
 } 
 
 void ThrowHit()
 {
     
     
     animation.Play("attack1");
     if(distenemy<3)
     {
     bloodfab = Instantiate(bloodfab, enemy.transform.position , enemy.transform.rotation) as GameObject;
     character_stats.health-=Random.Range(12,31);
     char_movesys.moveDir.y=15;
     }
     
     cooldown=3;
     
 }

}

Ive tried to set the bloods GameObject var to the bloods prefab at Update method but it still shows "Missing (Game Object)". It's like the prefab removes it'self temporaly from unity untill the game stops. Please help me out.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by dannyskim · Dec 05, 2011 at 08:36 PM

The easiest thing to do would be to create a private variable that is a placeholder for your bloodprefab object. You have your variable declaration:

  public GameObject bloodfab;

so what you can do is have another variable declaration:

 private GameObject bloodfabInstance;

and change your Instantiation to:

 bloodfabInstance = Instantiate(bloodfab, enemy.transform.position , enemy.transform.rotation) as GameObject;

Granted, this is the quickest and probably dirtiest way to fix your issue, but it will get the job done.

Technically, when the public GameObject variable gets destroyed and you get a Missing GameObject exception reference, this is proper behavior inside of the Unity Engine. You're destroying that instance of the variable inside of memory when Destroy() is called. Because of this and the way Instantiations and Destroys occur, it has become fairly normal practice to use a Pool Manager system to preload several instances of your GameObject that are held in an array and managed by a singleton when you first load the level. This alleviates memory allocation and reallocation, which if kept unchecked will eventually lead to excessive Garbage Collection and stutters in your frame rate. If you're not familiar with arrays or standard List types, you should learn about them and understand their concepts and usage as soon as possible.

Also, declaring the variable in Update to fix this issue would actually be the worse thing to attempt, because that's wasted processing allocation.

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
avatar image
0

Answer by benk0913 · Dec 06, 2011 at 12:08 PM

Ive already tried that, it didn't work. )= The place-holder calls itself 'Missing' too. I think it's because of the particle system in the prefab but I can not be sure. And redeclaring it at the update didn't work so I removed it but thanks for the tip, please help me out with this problem it holds me for a few days in the same position with that case.

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
avatar image
0

Answer by benk0913 · Dec 07, 2011 at 11:31 AM

I think I didn't get you correctly ccause it still does the same.

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can I use AddComponent to add (this.script) to an object? 1 Answer

AI control C# 1 Answer

Why doesn't this AI script work? c# 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