- Home /
Open door = load scene c# issues
Ok so I'm attempting to teach myself this as I go but its so complicating!! :P I've been trying to fix up this script by watching tutorials, forums, the unity manual, and nothing is helping! :( I can't seem to understand what the problem is, so I hope someone helps me out and doesn't decide to freeze this question too, as I've been searching for days now. I don't want an animation to play or anything. I just have a box collider set up in front of the door and I plan on having a line that pops up as you enter, saying "press 'button' to enter". Then I'll have a load screen and so on and so forth. But my main goal is to just proceed to the next scene, which would be the inside of the house. I get this error every time I try to use the script:
Assets/scripts/Scene_Prctice.cs(9,30): error CS0120: An object reference is required to access non-static member UnityEngine.Collision.gameObject'
 using UnityEngine;
 using System.Collections;
 
 public class Scene_Prctice : MonoBehaviour 
 {
     public void OnTriggerEnter (Collision collision)
     {
         if(Collision.gameObject.CompareTag == ("Player"))
            {
             Application.LoadLevel ("noWhere");
         }
     }
 }
Answer by Jeff-Kesselman · Jun 11, 2014 at 07:48 PM
Collision.gameObject is wrong. Thats a reference to a static method on the class Collision (which doesn't exist)
What you want is collision.gameObject, thats a reference to a method on the instance passed in.
Answer by abhishek082007 · Jun 11, 2014 at 08:09 PM
if(Collision.gameObject.transform.name == ("name of the Triger near door")) { Application.LoadLevel ("noWhere"); }
remember u need to add the scene in build settings.. only then " Application.LoadLevel ("noWhere"); " function will work
Answer by Prasanna · Jun 25, 2014 at 07:45 AM
Try this,
 using UnityEngine;
 using System.Collections;
 
 public class Scene_Prctice : MonoBehaviour 
 {
     public void OnTriggerEnter (Collider collision)
     {
         if(collision.gameObject.tag == "Player")
         {
             Application.LoadLevel ("noWhere");
         }
     }
 }
about Trigger Go http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
-Prasanna
Answer by BlastOffProductions · Dec 27, 2016 at 06:26 PM
Updated answer with SceneManager.
 using System.Collections;
 using UnityEngine.SceneManagement;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LoadCOl : MonoBehaviour {
 
     void OnTriggerEnter (Collider other)
     {
         if(other.gameObject.CompareTag("Player"))
         {
             SceneManager.LoadScene(1);
         }
         
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Change scene with trigger collision not working. 1 Answer
Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ? 1 Answer
How to enter trigger a certain amount of times before executing code? 1 Answer
Script calling onCollisionEnter2D and onTriggerEnter2D while disabled 1 Answer
My spikes don't damage the player 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                