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 Matt 5 · Jan 13, 2012 at 04:46 PM · scene

Same Script in a different Scene with different conditions

I have this code that has been posted on here quite a few times and changed as I go along with it. What I need now is to use this same code in a different scene, only I want it to load a different level than what it has on it now.

If anyone could help me with that it would be great.

Here is the code once again:

using UnityEngine; using System.Collections;

public class DamagableObject : MonoBehaviour { public bool m_Invincible = false; public float m_MaxHealth = 100f; public string WinScreen; [SerializeField] public float m_Health = 100f; GUISkin scoreSkin;

 public DecayingObject m_DeathEffectPrefab;
 public DecayingObject m_RemainsPrefab;
 public string m_DeathAnimation;
 
 /*public virtual float health
 {
     set
     {
         if (!m_Invincible ||  m_Health < value)
         {
             m_Health = value;
         }
         if (m_Health <= 0)
         {
             Death();
         }
         else if (m_Health > m_MaxHealth)
         {
             m_Health = m_MaxHealth;
         }
     }
     
     get
     {
         return m_Health;
     }
 }*/
 
 public virtual void DealDamage(float val, Vector3 pos, Vector3 normal)
 {
     if (val > 0f && !m_Invincible)
     {
         m_Health -= val;
         if (m_Health <= 0)
         {
             Death();
         }
     }
 }
 
     
 
 public virtual void Heal(float val)
 {
     if (val > 0f)
     {
         m_Health += val;
         if (m_Health > m_MaxHealth)
         {
             m_Health = m_MaxHealth;
         }
     }
 }
 
 public float GetHealth()
 {
     return m_Health;
 }
 
 protected void Start() {
     bool error = false;
     if (m_MaxHealth <= 0)
     {
         Debug.LogError("Max health can't be less than or equal to 0");
         error = true;
     }
     if (m_Health > m_MaxHealth || m_Health <= 0)
     {
         Debug.LogError("Invalid health range expected 0-" + m_MaxHealth + " got " + m_Health);
         error = true;
     }
     if (error)
     {
         Debug.Break();
     }
 }    
 
 
 
 public void Death() 
 {
     if (m_DeathEffectPrefab != null)
     {
         Instantiate(m_DeathEffectPrefab, transform.position, transform.rotation);
     }
     if (animation != null && animation[m_DeathAnimation] != null)
     {
         animation.CrossFade(m_DeathAnimation, 0.5f, PlayMode.StopAll);
         if (m_RemainsPrefab != null)
         {
             StartCoroutine("WaitForDeathAnimation", animation[m_DeathAnimation].length);
         }
     }
     else
     {
         
         LogicalEvent(m_RemainsPrefab);
         
         //if (m_RemainsPrefab != null)
         //{
         //    Instantiate(m_RemainsPrefab, transform.position, transform.rotation);
         //}
         //Scoring.m_score = 5;
         //Destroy(gameObject);
         
     }
 }
 
 void LogicalEvent(DecayingObject m_RemainsPrefab)
 {
     if (m_RemainsPrefab != null)
         {
             Instantiate(m_RemainsPrefab, transform.position, transform.rotation);
         }
                 Destroy(gameObject);
                 Scoring.m_score += 10;
 }
 
 
 protected IEnumerator WaitForDeathAnimation(float time)
 {
     yield return new WaitForSeconds(time);
     Instantiate(m_RemainsPrefab, transform.position, transform.rotation);
     Destroy(gameObject);
 }
 //public Texture2D background = null;
 //public Texture2D healthbar = null;

 
 void OnGUI()
 {
     //Draw Background
         //GUI.skin = scoreSkin;
         GUI.color = Color.red;
     GUI.Box(new Rect(55,5,100,20),Scoring.m_score.ToString());
      if(Scoring.m_score == 180)
     {
             Application.LoadLevel("LeftLung");    
     }
     
     
     
     
        
     //GUI.DrawTexture(new Rect(25.0f,25.0f,120.0f,14.0f),background);
         //GUI.color = Color.red;
     //Draw Healthbar
     //GUI.BeginGroup(new Rect(30.0f,30.0f,115.0f *(m_MaxHealth / m_Health),10.0f));
     //GUI.color =Color.red;
     //GUI.DrawTexture(new Rect(0.5f,0.10f,120.0f,10.0f),healthbar);
                 
     //GUI.EndGroup();
 }
     
     
 void Update(DecayingObject m_RemainsPrefab)
 {    
         LogicalEvent(m_RemainsPrefab);
     Scoring.m_score ++;
     
 }
 

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Jan 13, 2012 at 04:59 PM

Assuming this code is going to be on several instances, both player and AI, you need a third object, like a GameManager, who is going to know what is the next scene. For each of your scene, you create that object and set up the nextLevel (or whatever) variable, then in your character script get the manager in some way (Find, in the editor etc) and finally (OnGUI) :

  if(Scoring.m_score == 180)
 {
      Application.LoadLevel( gameMgr.nextLevel );   
 }
Comment
Add comment · Show 1 · 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 Matt 5 · Jan 13, 2012 at 05:08 PM 0
Share

How would I set something like that up I have never done something like this before. I am still new to Unity. I learn visually so its hard to just be told something. If you wouldn't $$anonymous$$d showing me what something like that would look like I would really appreciate it.

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

Endless DeathZone??!!? 4 Answers

How to load a scene on collision 1 Answer

Scene Change on Player Collision 1 Answer

Creating a scene using script (which Start() to use) 2 Answers

Calling function from another scenes gameobject 0 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