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 Karsnen_2 · Dec 09, 2011 at 12:03 AM · gameobjectprefabvariable

Prefab (Project Folder) to a GameObject (on the Scene)

Hello,

I am trying call another function which is attached to a gameobject( On the scene). But I want to call that function from a Prefab. This prefab is instantiated during run time.

Reason:

I am checking for collision on one script (on Prefab). If there is a collision on one then I want to call another function (on a gameobject) which would update the SCORE for me. In this way I could display variable carrying the score through OnGUI function.

Code:

The script on the prefab - cannondestroy.js

 var detonateObject : GameObject; 
 var score : la_point;

  
 function OnCollisionEnter(hit : Collision)
 {
        if(hit.gameObject.tag == "floor" || hit.gameObject.tag == "enemy"  )
        {
     //Checks if floor is hit
     var detonate = Instantiate (detonateObject,gameObject.transform.position , Quaternion.identity);
       Destroy(gameObject); //Destroys this gameObject if true.

    /*
 
         if(hit.gameObject.tag == 'enemy')
 
         score.enemyhit();
 
         else
 
         //check for hattrick errors. score.enemymiss();
 
         */
       }
 }



Code Explanation:

As per the code - it checks for collision. It checks for collision both on the floor and enemy. If the enemy is a hit I wish to update the scoreline else if the floor is the hit I wish to increase the miss score. The other script that is attached onto to gameobject on every scene would be la_point.js.

My Perception:

I am using a lot of scenes. With the prefab fixed, I could alter the variables on the gameobect which is one the scene view for every scene. The script is going to the same but scores will start from zero always when a new level is loaded.

TRYOUTS:

I tried to attach the script which has the gameobject onto the script on the prefab (again on project folder). But it did not work. I see that I can't do that too.

I humbly REQUEST for help as I am trying solve this which is every important information that has to be given to the user.

THANK YOU very much in advance

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Dec 09, 2011 at 12:19 AM

You can use a static variables and functions to access the score script.

 // Score.js
 static var instance : Score;
 var score : int;
 
 function Awake() {
     instance = this;
 }

 static function EnemyHit() {
     instance.score += 10;
 }

 // YourOtherScript.js    
 function OnCollisionEnter(hit : Collision) {
     if(hit.gameObject.tag == 'enemy')
         Score.EnemyHit();
 }
   

 
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 Karsnen_2 · Dec 09, 2011 at 12:25 AM 0
Share

But I wish to bring down the values to zero every time I load a new scene.

So when I load a new scene should I just call a random awake function to revert back the values to 0?

avatar image Karsnen_2 · Dec 09, 2011 at 12:29 AM 0
Share

instance = this;

Could I please know what the above statement actually does?

avatar image
1

Answer by Tpaley · Mar 24, 2013 at 04:24 PM

you could do:

 //Other stuff
     if(hit.gameObject.tag == 'enemy'){

 var script = GameObject.Find("name of game object. this could mean it's own name.").
 GetComponent(scriptName);
     score.scriptName =+ 10;
 
     }else{
 
     //Same thing but with other stuff. I think I told you everything needed.
 
     
   }
 }

hope that helps. I don't get static stuff myself so I don't bother with that. plus, unless you use DontDestroyOnLoad, you should be fine with it resetting.

I'm not completely sure that you can modify the variable but you probably can.

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

How do I assign a prefab reference to a private variable? 1 Answer

How to create 3D game object in specified pixel size? 2 Answers

How to add Forces and Torques relative to an object at a none central position 2 Answers

C# Check If Scripted Gameobject goes Past Variable Gameobject 0 Answers

Setting a game object from a list to be the active character that gets instantiated 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