• 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 leenalee92 · Nov 26, 2014 at 10:08 PM · javascriptbooleanloadlevel

Why my game just win all the way?

Assume that i have 2 cube as the game object named cube1 and cube2 and 2 transparency cube as collider named co1 and co2 when the cube1 hit the co1 && cube2 hit the co2 = win game

Here's My code of my MainScript

 var Cube1: GameObject;
 var Cube2: GameObject;
 
 function Update()
 {
     Cube1 = GameObject.Find ("Cube_1");
     Cube2 = GameObject.Find ("Cube_2");
 }
 
 function FixedUpdate()
 {
     if (C1.Collided == true && C2.Collided == true)
     {
      Application.LoadLevel("Level_2")
     }
 }

This is the boolean script for co1 and co2, another 1 assigned to co2 just change the cube_1 to cube_2

 static var Collided = false;
 
 function OnTriggerEnter (col : Collider)
 {    
     if(col.name == "Cube_1")
     {
         Collided = true;
         print ("Hit Cube_1");
     }
 }
 
 function OnTriggerExit (col : Collider)
 {
     if (col.name == "Cube_1")
     {
         Collided = false;
         print ("Cube_1 Left");
     }
 }

So now here's the problem, when i play my Level 1, everything goes fine, when i load to level 2 and my cube1 and cube2 haven hit the co1 and co2, it just win the game straight away and load to level 3. But if i direct jump to level 2,everything works fine, This problem happens in other levels as well, can anyone help me?

P/S: Sorry for my broken english

Thank You!

Comment
Add comment · Show 8
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 meat5000 ♦ · Nov 26, 2014 at 10:05 PM 0
Share

col.gameObject.name makes more sense. The collider is nameless.

You haven't posted the go to level 3 code.

avatar image leenalee92 · Nov 27, 2014 at 03:04 AM 0
Share

so i just need to change my game object name in level 2, 3 and so on?

avatar image leenalee92 · Nov 27, 2014 at 03:06 AM 0
Share

i though if we want to call function from another script we need to use static? because the error keep saying cannot reference to non-static member

avatar image leenalee92 · Nov 27, 2014 at 03:10 AM 0
Share

the code go to level 3 just almost same with the code go to level 2

avatar image meat5000 ♦ · Nov 27, 2014 at 03:30 AM 0
Share

GetComponent can fire a function from another script.

Show more comments

1 Reply

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

Answer by tanoshimi · Nov 26, 2014 at 11:08 PM

col.name is fine. All components share the same name as the gameobject to which they are attached.

http://docs.unity3d.com/ScriptReference/Collider.html http://docs.unity3d.com/ScriptReference/Object-name.html

I suspect this due to the use of static variables (why?). You say you attach he same script to each cube, so the class-level static variable "Collided" will be being shared between both cubes.

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 leenalee92 · Dec 02, 2014 at 01:39 PM 0
Share

Hi tanoshimi, sorry for bothering you. Is there any solution for me to reset my game after i clicked restart button? Cause it just win straight away after i clicked restart level 1. I don't want to change the gameObject name, is there any solution for me to reset the game?

avatar image leenalee92 · Dec 02, 2014 at 05:14 PM 0
Share

After i change my static var to public and use getcomponent in my main script. i get this error >> NullReferenceException, can you please help me?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

LoadLevel Health Question. 1 Answer

Simple gun draw script not working 1 Answer

JS Unity Boolean 'not possible to evoke an expression of type 'boolean'' 1 Answer

Stuck while solving a Coroutine problem 0 Answers

how to know which script you change? 3 Answers


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