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 Romano185 · Feb 11, 2012 at 06:15 PM · instantiateprefabgetcomponentfindwithtag

Variable of prefab has not been assigned

Hi everybody

I've made a script which needs 2 variables to work, its own transform and the transform of an prefab with the tag Player. But the script can't reach the transform of the prefab which is instantiated by this script:

    var classselected : boolean = false;
     var spawnplace : Transform;
     var archerobject : GameObject;
     var mageobject : GameObject;
     var warriorobject : GameObject;
     
     function OnGUI () {
     
     if(classselected == false){
     GUI.Box(Rect(0,0,300,500),GUIContent("Choose your class wisely"));
     }
     
     if(GUI.Button(Rect(100,100,100,100),GUIContent("Archer")) && classselected == false ){
     var instance : GameObject = Instantiate(archerobject, spawnplace.position, spawnplace.rotation);
     classselected = true;
     }
     
     if(GUI.Button(Rect(100,200,100,100),GUIContent("Mage")) && classselected == false){
     var instance2 : GameObject = Instantiate(mageobject, spawnplace.position, spawnplace.rotation);
     classselected = true;
     }
     
     if(GUI.Button(Rect(100,300,100,100),GUIContent("Warrior")) && classselected == false){
     var instance3 : GameObject = Instantiate(warriorobject, spawnplace.position, spawnplace.rotation);
     classselected = true;
     }
     }
    
     function Update (){
     if (classselected == true){
     Destroy(gameObject);
     }
     }

This script instantiates 3 different prefabs, all with the tag Player. And this is the script which needs the variable othertransform, the transform of 1 prefab with the tag Player:

 static var aggroDistance = 10;
 var mytransform : Transform;
 var otherobject : GameObject;
 var othertransform : Transform;
 var glitchCorrector : boolean = true;
 var tickerInterval : boolean = false;
 var tickerRate = 5.0;
 private var nextTick = 0.0;
 var drop : GameObject;
 static var enemyHealth : int = 100;
 static var seppuku : boolean = false;
 
 
 function Update () {
 
 othertobject = GameObject.FindWithTag("Player");
 othertransform = otherobject.GetComponent(Transform);
 
 if (Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && glitchCorrector == true){
 ArcherHealthScript.curhealth += 10;
 WarriorHealthScript.curhealth += 10;
 MageHealthScript.curhealth += 10;
 glitchCorrector = false;
 }
 
 if (Vector3.Distance(mytransform.position, othertransform.position) > aggroDistance && glitchCorrector == false){
 glitchCorrector = true;
 }
 
 if(Vector3.Distance(mytransform.position, othertransform.position) < aggroDistance && tickerInterval == false && HealthScript.curhealth > 0){
 ArcherHealthScript.curhealth -= 10;
 MageHealthScript.curhealth -= 10;
 WarriorHealthScript.curhealth -= 10;
 tickerInterval = true;
 }
 
 if(tickerInterval == true && Time.time > nextTick){
 nextTick = Time.time + tickerRate;
 tickerInterval = false;
 }
 
 if(enemyHealth <= 0){
 seppuku = true;
 Destroy(gameObject);
 var instance : GameObject = Instantiate(drop, transform.position+Vector3(0,0.2,0), transform.rotation);
 ExperienceScript.curexp += 10;
 }
 }

I think the problem is FindWithTag or .GetComponent, but I'm not very sure.

Please help me solve this problem!

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 Berenger · Feb 11, 2012 at 07:32 PM 0
Share

Where is the error raised ? othertransform is null ? Have you tried the function FindGameObjectWithTag ? I'm not quite sure of the difference between the two ...

avatar image Romano185 · Feb 11, 2012 at 07:55 PM 0
Share

It just says in the error bar: Variable otherobject has not been assigned. It also displays: Variable othertransform has not been assigned. So I think the mistake is in the FindWithTag function. FindGameObjectWithTag also doesn't work. I'm really sure the Prefab has the tag Player.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Feb 11, 2012 at 08:12 PM

Look's like your instanciation occurs only when the player presses a button. Problem is, in your Update, you are looking for it all the time. FindWithTag will only look for objects instanciated, in the scene, not the prefab in your project folders.

So you should create a public function in the second script to affect those other's variables, and call it from the other script. In the first script's update, check if othertobject is null and leave the function if it is.

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 Romano185 · Feb 12, 2012 at 01:00 PM

Problem solved! Just made a small mistake, said other*t*object instead of otherobject. But I've made classselected static and added if(classselected == true) to the second script right before FindWithTag, just to be safe

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Get unity to recognize prefab in C# 2 Answers

Set a variable in another object at point of instantiation. 1 Answer

Instantiate Prefabs. Errors. 2 Answers

Play iTween on instantiated prefab 1 Answer

Access a function of an instantiated prefab 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