Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collision into Character Change

Discussion in '2D' started by Nicreven, May 22, 2019.

  1. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Hi, I'm working on my first 2D game and have a cool idea for a mechanic in mind that would be used for solving puzzles, killing various mobs and bosses, etc.

    The idea is that when you collide with an enemy you lose control of the player character and get control of the enemy you collided with. If you collide with the player character you lose control of the enemy and get control of the character once more.

    I've made the collision script but, I'm not sure how to code in the mechanic all that well.
    I have it set as a boolean variable and if it's set to true, the controls are allowed to work, same for the enemy but with a different name for the variable.

    I just need to know how to code in the communication between the two, since I already have the collision part of the code set.
    Thank you in advance.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    It's best to show the code you've already written rather than try to describe it. (if you do, use the forum code tags to make the code nicely formatted)

    I can think of two pretty straightforward ways to do this:

    Have a component that receives your input and applies movement to the object. Put that component on anything you could control. Any object not being controlled should have that component disabled. When you collide (OnEnter), you disable the current one and enable the one on the object you collided with. There should only be one of those components enabled at any given time.

    A common and more robust (but also more difficult) approach for this type of system would be to create a Controller script which receives your inputs, and forwards the inputs to the currently controlled target. Each controllable target will have a component that can receive commands from the Controller, either directly or via an interface that is implemented (like "IControllable" or something). I realize that you're a new developer and it may not be clear to you how to do that, but I'm happy to help if you'd like to know more.
     
  3. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(MovementEnemy))]
    6. [RequireComponent(typeof(SpriteRenderer))]
    7. public class Movement : MonoBehaviour
    8. {
    9.    public bool Control = true;
    10.    public SpriteRenderer sr;
    11.    public float XSpeed = 5f;
    12.    public float JumpForce = 2f;
    13.    public Rigidbody2D rb;
    14.  
    15.    private bool grounded;
    16.    public Transform groundCheck;
    17.    public float checkRadius;
    18.    public LayerMask Ground;  
    19.  
    20.    private int Jump;
    21.    public int JumpValue;
    22.      
    23.     void Start()
    24.     {
    25.         rb = GetComponent<Rigidbody2D>();
    26.         sr = GetComponent<SpriteRenderer>();
    27.         Jump = JumpValue;
    28.     }
    29.  
    30.     void FixedUpdate()
    31.     {
    32.         grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, Ground);
    33.    
    34.         float Xmov = Input.GetAxis ("Horizontal");
    35.     if(Control == true){  
    36.         rb.velocity = new Vector2 (Xmov * XSpeed, rb.velocity.y);
    37.  
    38.        
    39.  
    40.         if(Xmov < 0){
    41.            sr.flipX = false;
    42.         }else if(Xmov > 0){
    43.            sr.flipX = true;
    44.         }
    45.  
    46.         if(Input.GetKeyDown(KeyCode.UpArrow) && Jump > 0 && grounded == true){
    47.           rb.velocity = Vector2.up * JumpForce;
    48.           Jump --;
    49.         }
    50.         if(grounded == true){
    51.            Jump = 1;
    52.  
    53.         }
    54.  
    55.      
    56.     }
    57. }
    58. }
     
  4. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    There's the code, I made a second script (copied it but changed the Control variable name to EnemyControl) and assigned it to the enemy.

    Wanted to add that the more robust method, as you call it, seems a bit easier to understand (probably harder to code) and a bit more logical.
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I agree. You can think of this kind of system as the mario & cappy system from Mario Odyssey. Since I understand that it's a pretty damn hard to just "try" a system like that for the first time, here's an example framework that you can try and expand on and hopefully learn some stuff from:

    The interface:
    Code (CSharp):
    1. public interface IControllable
    2. {
    3.     GameObject gameObject { get; } // monobehaviours already implmement this by default
    4.  
    5.     // these must be implemented by the controllable object
    6.     Controller Controller { get; set; }
    7.     void OnControlStart();
    8.     void OnControlEnd();
    9. }
    The controller:
    Code (CSharp):
    1. public class Controller : MonoBehaviour
    2. {
    3.     // optionally take control of something right at the start
    4.     [SerializeField] private GameObject _controlAtStart;
    5.  
    6.     // the currently controlled object
    7.     public IControllable CurrentTarget { get; private set; }
    8.  
    9.     private void Start()
    10.     {
    11.         if (_controlAtStart != null)
    12.         {
    13.             TakeControl(_controlAtStart);
    14.         }
    15.     }
    16.  
    17.     public void TakeControl(GameObject controllableObject)
    18.     {
    19.         IControllable controllable = controllableObject.GetComponent<IControllable>();
    20.         if (controllable != null)
    21.         {
    22.             // release control of the current object before controlling the new one
    23.             ReleaseControl();
    24.  
    25.             // assign the new target
    26.             CurrentTarget = controllable;
    27.             // tell it it's being controlled now (by this controller)
    28.             CurrentTarget.Controller = this;
    29.             CurrentTarget.OnControlStart();
    30.  
    31.             Debug.LogFormat("Now controlling {0}", controllable.gameObject.name);
    32.         }
    33.     }
    34.  
    35.     public void ReleaseControl()
    36.     {
    37.         // if there is something to release control of
    38.         if (CurrentTarget != null)
    39.         {
    40.             // tell it it's no longer being controlled and clear the reference to it
    41.             CurrentTarget.OnControlEnd();
    42.             CurrentTarget.Controller = null;
    43.             CurrentTarget = null;
    44.  
    45.             Debug.LogFormat("Released control of {0}", CurrentTarget.gameObject.name);
    46.         }
    47.     }
    48. }
    Something controllable:
    Code (CSharp):
    1. // example of a controllable thing in the game
    2. public class PlayerCharacter : MonoBehaviour, IControllable // heres the interface
    3. {
    4.     // part of interface, assigned by Controller
    5.     public Controller Controller { get; set; }
    6.  
    7.     private bool _beingControlled = false;
    8.  
    9.     // part of interface, called by Controller
    10.     public void OnControlStart()
    11.     {
    12.         _beingControlled = true;
    13.     }
    14.  
    15.     // part of interface, called by Controller
    16.     public void OnControlEnd()
    17.     {
    18.         _beingControlled = true;
    19.     }
    20.  
    21.     private void Update()
    22.     {
    23.         if (_beingControlled)
    24.         {
    25.             // get inputs, move, etc.
    26.         }
    27.     }
    28. }
    How you might control something when you enter a trigger on it:
    Code (CSharp):
    1.  
    2. // this component would go on the PLayerCharacter object for example
    3. public class ControlOnCollision : MonoBehaviour
    4. {
    5.     private IControllable _controllable;
    6.  
    7.     private void Awake()
    8.     {
    9.         _controllable = GetComponent<IControllable>();
    10.     }
    11.  
    12.     private void OnTriggerEnter2D(Collider2D other)
    13.     {
    14.         // if this object is currently being controlled
    15.         if(_controllable.Controller != null)
    16.         {
    17.             // if the trigger we entered is on an object that is controllable
    18.             if (other.GetComponent<IControllable>() != null)
    19.             {
    20.                 // tell this object's controller to take control of it
    21.                 _controllable.Controller.TakeControl(other.gameObject);
    22.             }
    23.         }
    24.     }
    25. }

    That last component could go either way, the other object takes control, or the current object gives control. Or perhaps each object just tells the controller that it collided with another controllable, and the controller decides what to do.

    I opted to have each controllable thing contain it's own logic for taking inputs etc, because every controlled object could behave differently, so that would make it easy to have a car use different inputs than a human, etc.

    This is a bit more than I originally intended to write, and I didn't actually test it. But hopefully you can read through this and understand what is going on and why. Let me know if you have any questions about it.
     
    Last edited: May 24, 2019
  6. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Reading through the first one, the 6 or so lines just gave me a tiny headache because I haven't really encountered things such as these. (Like for instance what the hell is Controller Controller supposed to mean? Is Controller a class and a class name I have no clue what the actual hell is going on)

    I'll uhh... stick to getting the basics down, I have been procrastinating on learning code because I thought it looked a little tough. I didn't want to be babied through it and not learn anything either
    It's really tough finding a middle ground, you know?
     
    LiterallyJeff likes this.
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I get where you're coming from. I definitely don't mean to overwhelm you. These classes are pretty dense, and written the way I write code, not how a beginner would write code.

    To be honest I think the best way to learn is just to keep asking questions until you get it. In the grand scheme of things, this is a pretty straightforward system using some fundamental programming techniques. I'd be happy to explain anything in these scripts to you if you think it will help you learn.

    So just for a little background: An interface is a set of functions. If a class implements the interface, that forces the class to define those functions. This is great because we can define an interface for a controllable object, which any class can implement (player, enemy, car, bird, etc). Then the controller can check if a component implements that interface, and it will know it has those functions, and can use them to control it without needing to know what it actually is. (this is not the only way to accomplish this behavior, but it's a good way to do it)

    Controller Controller { get; set; }


    So this is a C# property. It's like a variable, but it consists of two functions, the Get and Set which will be called if you try to access it, or assign it. In this case they don't do anything special, so it behaves the same as a normal variable but can be added to an Interface (because interfaces are a set a functions, and won't accept variables).

    Controller is the class I defined in the next code section. I also (perhaps confusingly) named the property Controller as well. They're both capital case because that's the C# standard for properties as well as class names.

    So "Controller Controller" is a property named Controller, which holds a reference to a Controller. That becomes a lot more clear with proper syntax highlighting, because the class name would be a different color.

    But you can think of it like this if it helps:
    public Controller MyController { get; set; }
     
    Last edited: May 28, 2019
  8. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Yeah I've been procrastinating on code a lot and I really wanna get into it now
    Summer break is coming up anyways, so I wanna develop this for the length of it so I can get into more ambitious projects.

    One thing I want to ask: Why did you name the first script "IControllable" instead of something like "Controllable"?
     
  9. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Oh, also (sorry for being so unorganized, I just keep remembering things I want to ask), what's the difference between public, private and [SerializeField] + private?
    Like, aren't public and private ( with SerializeField ) the same?
     
  10. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Adding the prefix I is standard or very common naming convention for interfaces to identify them as an interface class as opposed to a normal class without needing to go to the definition.

    public and private are two of a group of access modifiers. They dictate what can access that thing. Classes, functions, variables, pretty much anything can have its access controlled by these keywords. It's useful for describing how you want certain things to be used in the class, or what things should definitely not be used or altered from outside the class.

    It may not be clear what or why you would need that right now, but you start to pick that up as you go. My rule is make everything private until it needs to be public. (in fact, if you don't put an access modifier, it defaults to private)

    private means only this class can access it, it's hidden from the outside world. If you had a reference to the class, it wouldn't show up in the intellisense list and you'd get a compiler error if you tried to access it.

    public means anything with a reference to that class can access it. It also has a magic unity feature associated with it, where variables that are public will be shown in the inspector for the component. Values you set in the inspector will override the default value for that variable.

    That unity feature only works for "serializable" types, such as primitive types (int, float, bool, string) or classes with [Serializable] before their definition, and im sure some other things im forgetting. Basically unity will save those values into the text file for that component in the prefab. You can actually open up your prefab assets and see the serialized text format. It's not that important to fully understand that yet.

    However, that's where [SerializeField] comes in. If you have a value that you don't want other classes to access, you make it private. That means Unity won't serialize it, it won't show up in the inspector. But if you still want to be able to configure it in Unity in the inspector, you can add [SerializeField] before it, and then Unity will pick it up again. So you get the best of both worlds. There's also a [HideInInspector] you can use for public fields. There should probably be an equivalent [ShowInInspector] to be more beginner friendly, but alas.

    There are more access modifiers like protected which only allows child classes to access it, but those aren't nearly as common. (but still very useful)

    That is a pretty quick and dirty explanation of it, so hopefully it makes sense.

    In the grand scheme of things, if you're the only person reading and using your code, you can do whatever you want. This is mainly a tool for making code maintainable and understandable for other people, or far in the future when not even you remember what you were thinking.
     
    Last edited: May 28, 2019
  11. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    So if I make them all public, it doesn't really matter? (as long as I'm the only one looking at the code, which I will be)
     
  12. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Oh yeah, I wanted to ask, how do I get it so that 2 of my scripts in unity can communicate? By that, I mean that 1 script alters variables from the other and vice versa.
     
  13. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Technically speaking any codebase could remain identical but have everything public, and it would work the same.

    It really depends on how you use it. If you have public fields/functions everywhere, you're probably going to end up with a lot of code reaching all over the place and accessing anything/everything. That's called spaghetti code. Hard to debug, hard to reason about, and lacks consistency because it's almost random in nature. It's a lot easier to manage code when there's a clear way it's intended to be used, which is described by what is public and what is private. Hope that makes sense. That's why I default to private, and only when something needs access to something else, then I'll make it public, or create some way for the private data to be accessed, like a function or property.
     
  14. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    To communicate between components directly, one needs a reference to the other. There's several ways to get that reference, and depending on what they are doing, one way might be better/easier than another. Knowing which way comes with practice.

    Are the two objects physically interacting (collision/raycast)? Is it purely logical interaction(opening UI, saving/loading, getting a cell of a tilemap)? Are the scripts part of the same object (player using a reference to his rigidbody)? Each of these questions probably have different approaches for getting the reference.

    For example, if you want to have an enemy collide with the player and reduce its health, you could have the collision event be the way you get the other object, so if an Enemy collides with something, you just get the Collider reference from the event:
    Code (CSharp):
    1. public class Enemy : MonoBehaviour
    2. {
    3.     [SerializeField] private float _damageOnHit; // assigned in the inspector
    4.  
    5.     private void OnTriggerEnter2D(Collider2D other)
    6.     {
    7.         // check if the trigger has a player component
    8.         Player otherPlayer = other.GetComponent<Player>();
    9.         if (otherPlayer != null)
    10.         {
    11.             // if it does, call the ApplyDamage function
    12.             otherPlayer.ApplyDamage(_damageOnHit);
    13.         }
    14.     }
    15. }
    16.  
    17. public class Player : MonoBehaviour
    18. {
    19.     [SerializeField] private float _maxHealth = 10; // assigned in the inspector (defaults to 10)
    20.     private float _health; // private so things cant directly change health
    21.  
    22.     private void Awake()
    23.     {
    24.         _health = _maxHealth; // initialize the health value
    25.     }
    26.  
    27.     public void ApplyDamage(float amountOfDamage)
    28.     {
    29.         // only allow positive amounts of damage or 0
    30.         // max chooses the higher value
    31.         amountOfDamage = Mathf.Max(0, amountOfDamage);
    32.  
    33.         // calculate what the new health value will be
    34.         float healthAfterDamage = _health - amountOfDamage;
    35.  
    36.         // choose whichever is greater, the new health or 0
    37.         _health = Mathf.Max(0, healthAfterDamage);
    38.  
    39.         if (_health == 0)
    40.         {
    41.             Die();
    42.         }
    43.     }
    44.  
    45.     private void Die()
    46.     {
    47.         // handle death or respawning
    48.     }
    49. }

    If both objects exist in the scene together from the very start, you could have a variable declared that holds a reference to that other object's component, assigned in the inspector and saved in the scene. The variable could be written with a type of "Player" or "Enemy" directly.

    You could have one component that is responsible for keeping track of many important related references. It's pretty common to have a helper class that exists in the scene (sometimes the one component existing throughout all scenes), purely to aid in finding references to important components, or having convenient functions to call (like Pause, or Save & Load).

    These types of decisions are what ultimately define your style of programming, the approaches you enjoy most. Some people always go for the most optimal design patterns, some people code by intuition and what makes the most sense to them, some people do whatever they want. In the end, if your game works, it doesn't really matter.
     
    Last edited: Jun 1, 2019
  15. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    How the hell did you learn C#?
     
  16. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Using Unity and following youtube tutorials, then getting a job and doing it every day.

    My learning process since the beginning, until now, goes like this:
    1. Try to do a thing
    2. Hit a roadblock
    3. Google stuff for more info/examples/solutions
    4. Repeat until it works
    If I go thru this a lot and can't figure it out, I post on these forums. The key is to not get frustrated, expect the roadblocks, and try to never get stuck on the same roadblock twice.
     
    Last edited: May 29, 2019
  17. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    That was generally my idea, but I gigantic project like this mechanic (seeing as I literally only touched c# once, learning the really really simple useful stuff)
    I'll stick to basics for now and try to make some functional platformers first.
     
  18. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Sounds good dude, it will all make sense in time.
     
  19. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Thanks for all the help man, cheers
     
  20. orxyandcrake

    orxyandcrake

    Joined:
    Feb 15, 2019
    Posts:
    10
    Without having read any of the responses, this is what I would do.
    There is no player or enemy. There is only units... A unit contains both the code for the player and an enemy. Then, this is the crazy part... all you need is a single bool isPlayer. On collision current isPlayer is set to false and the unit (enemy collided with) isPlayer is set to true... this will seamlessly swap control from one entity to the next.

    This should make it a lot simpler than you may have first thought.

    Hopefully this makes sense without having to explain how the isPlayer bool effects the Unit code... if not let me know, and i'll explain further.

    I use this approach all the time, so I know it works in practice.

    In general make everything generic and then layer on any unique attributes as late as possible.
    Always aim to make you code reusable.
     
    Last edited: Jun 2, 2019
  21. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    My original idea was pretty much this: (simplifying code because I don't want the whole code in here)
    using blah blah

    namespace blah blah

    variables or whatever
    public bool Controlled = true;

    other stuff
    if (controlled = true)
    {
    movement
    }

    Did this for both ( sorry if it's a stupid approach to explaining it)
     
  22. 41northstudios

    41northstudios

    Joined:
    Aug 8, 2013
    Posts:
    5
    You'd be well served doing a few intro courses to C# and object oriented programming (OOP) -- there are some core concepts you need to learn that will help make the rest of this start to make sense. It's also good to develop good habits and programming practices early on in your education - it will save you a TON of headaches down the line.

    There are a ton of online resources out there - Google "beginner C#" or "beginner object oriented programming concepts" and you'll find a ton of YouTube videos. If you have access to some online training sites (Pluralsight, LinkedIn/Lynda, Udemy, etc) you can find some good courses on there. There are also a ton of great books out there - some classics I'd recommend are Code Complete and Clean Code.

    Hang in there, programming can be tough to get your head around but it becomes easier over time (and a lot of the concepts you learn are language agnostic). When I started I would make little programs to learn concepts, nothing nearly as complex as some Unity scripts can get. This is where a course can help as they will introduce concepts and show examples in a logical order that builds on itself.

    Good luck in your studies!
     
  23. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Thanks mate, but, I'm not really a patient guy whatsoever;
    I'm going into it head first with barely a shred of prior knowledge.
    If I run into a problem, I google it.

    I have decided that I'm not gonna be watching any tutorial videos, at least not thoroughly. The reason for this being the fact that youtubers just write the code, some explain it but, generally they don't do a great job at it. I'm sticking to forums and all that kind of stuff.

    It's been working out great so far, so I'll stick to this while I'm learning.

    Btw this already makes sense to me, I get the gist of what everything does(every function and class that I've encountered so far). Anyways, thanks for all the help.
     
  24. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Alright guys I actually managed to do it, I made the collision work ( I just needed to type in 2D)
    I also figured out on my own how to make a switch-gravity mechanic.
    The only problem I'm having now is "Object reference is not set to an instance of an object"
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.  
    8.    public float speed = 5f;
    9.  
    10.    public Rigidbody2D rb;
    11.  
    12.    public EnemyMov ecc;
    13.  
    14.    public bool controlled = true;
    15.  
    16.     void Start()
    17.     {
    18.       rb = GetComponent<Rigidbody2D>();
    19.       ecc = GetComponent<EnemyMov>();
    20.    
    21.     }
    22.  
    23.     void OnCollisionEnter2D(Collision2D col)
    24.     {
    25.        if(col.gameObject.name == "Enemy")
    26.        {
    27.          ecc._controlled = true; //error resides here.
    28.          controlled = false;
    29.        }
    30.     }
    31.    
    32.  
    33.  
    34.     void FixedUpdate()
    35.     {
    36.       //All things movement based
    37.       if(controlled == true)
    38.       {
    39.        float Ymov = Input.GetAxis("Vertical");
    40.        float Xmov = Input.GetAxis("Horizontal");
    41.  
    42.        Vector2 mov = new Vector2(Xmov, Ymov);
    43.  
    44.        rb.AddForce(mov * speed);
    45.        }
    46.        //gravity
    47.        if(Input.GetKeyDown(KeyCode.Q) && Physics2D.gravity != new Vector2(0f, 9.8f))
    48.        {
    49.          Physics2D.gravity = new Vector2(0f, 9.8f);
    50.        }
    51.        else if(Input.GetKeyDown(KeyCode.Q) && Physics2D.gravity == new Vector2(0f, 9.8f))
    52.        {
    53.          Physics2D.gravity = new Vector2(0f, -9.8f);
    54.        }
    55.       }
    56. }
    57.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class EnemyMov : MonoBehaviour
    7. {
    8.  
    9.    public float speed = 5f;
    10.  
    11.    public Rigidbody2D rb;
    12.  
    13.    public Movement pc;
    14.  
    15.    public bool _controlled = false;
    16.     void Start()
    17.     {
    18.       rb = GetComponent<Rigidbody2D>();
    19.       pc = GetComponent<Movement>();
    20.     }
    21.     void OnCollisionEnter2D(Collision2D col)
    22.     {
    23.      if(col.gameObject.name == "Player")
    24.       {
    25.        pc.controlled = true; //error resides here.
    26.        _controlled = false;
    27.  
    28.       }
    29.     }
    30.    
    31.     void FixedUpdate()
    32.     {
    33.       if(_controlled == true)
    34.       {
    35.        float Ymov = Input.GetAxis("Vertical");
    36.        float Xmov = Input.GetAxis("Horizontal");
    37.  
    38.        Vector2 mov = new Vector2(Xmov, Ymov);
    39.  
    40.        rb.AddForce(mov * speed);
    41.        }
    42.     }
    43. }
    44.  
    45.  
    I have no clue how to manipulate variables from other scripts in another script, resorted to this.
     
  25. 41northstudios

    41northstudios

    Joined:
    Aug 8, 2013
    Posts:
    5
    All good, everyone has a different style of learning so do what works for you. My only point is - there are foundational concepts which will really help you to understand first. It’s one thing to grok what code is doing, but there are best practices and techniques that exist to keep your code clean and efficient. Can you code a game without learning that stuff? Sure, but you’ll find yourself in weird situations down the line where maintenance and bug fixes are a nightmare. Basically you can save your future self a ton of headaches if you at least brush up on basic OOP principles.

    I agree that a lot of YouTube tuts leave a lot to be desired - but if you search around you can find some good courses online.

    Anyway, that’s just my $0.02 so have fun!
     
    orxyandcrake likes this.
  26. 41northstudios

    41northstudios

    Joined:
    Aug 8, 2013
    Posts:
    5
    This is similar to how I would approach it also. OP’s current solution now means he has 2 scripts with identical functionality he has to maintain.
     
  27. Nicreven

    Nicreven

    Joined:
    May 22, 2019
    Posts:
    15
    Question:
    How do I manipulate variables from script1 in script2?

    (I.E script1 has a boolean variable nascar, I want to set nascar to true in script2 and have it change in script1 without previously assigning it in script2)